I have a small problem, I have an object that I attached a class. In this class I have an Enum ... which determines the type of object that I want to create. The problem is that when I start the game then I stop it, another object is created and it does not destroy the other. Each time, starting and stopping the game, another object is created. The whole problem is in the Update method. imagwe
using EduUtils.Events;
using EduUtils.Visual;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class TrainCell : ObjectSequence
{
public Transform[] Obstacles;
public bool used = false;
public bool hasRail = false;
public bool underBuilding = false;
public bool underTrain = false;
public bool hasObstacle = false;
[HideInInspector] public int oldIndex = 0;
public enum ShapeObstacleType
{
NONE,
ROCK,
TREE
}
public ShapeObstacleType currentObstacleType;
private ShapeObstacleType oldObstacleType = ShapeObstacleType.NONE;
private Transform currentObstacle;
private new void Update()
{
if (Application.isEditor && !Application.isPlaying)
{
if (currentObstacleType != oldObstacleType)
{
oldObstacleType = currentObstacleType;
if (currentObstacle != null)
{
hasObstacle = false;
DestroyImmediate(currentObstacle.gameObject);
}
if (currentObstacleType != ShapeObstacleType.NONE)
{
currentObstacle = Instantiate(Obstacles[(int)currentObstacleType - 1]);
currentObstacle.transform.parent = transform;
currentObstacle.transform.localPosition = Vector3.zero;
hasObstacle = true;
}
}
if (currentObstacle == null)
hasObstacle = false;
}
}
}
This code is executed in edit mode ... which is the problem if I check before creating the object. Check if it's not null, then destroy it. Thanks a lot.
publichas any affect at all while your are still inside the same class though ;) - derHugo