0
votes

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.

1
I found the problem....this {private ShapeObstacleType oldObstacleType = ShapeObstacleType.NONE;} it must be public. :) - George
I somehow doubt that changing it to public has any affect at all while your are still inside the same class though ;) - derHugo
Yes, if I change the access type everything goes perfectly, I have already tested several times. - George
Setting it as public will serialize it in the editor, which overrides what you initialize it as in the code, which may have non-obvious effects? That's all I can think of, if you're 100% sure you haven't changed ANYTHING else han private -> public. - Fredrik Schön
Also avoid destroying objects especially if there are many objects you want to destroy. Use object pooling instead. - Dave

1 Answers

0
votes

I found the problem....this:

private ShapeObstacleType oldObstacleType = ShapeObstacleType.NONE; This must be public. I don't know why, but I think [enum] doesn't change his type if he's private. I have no explanation for why it doesn't work if it's private and why it goes public.I'll document myself and come back with an explanation, or if others already have an explanation. Thank you!