0
\$\begingroup\$

I was trying to get familiar with Unity's ExecuteInEditMode

Everything is fine it keeps the old object although I have deleted them.

And one more thing, although every-thing (cube) is instantiated is set as child of GameObject script is attached to, they where the other cubes come from?

I have this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class Road : MonoBehaviour
{
    public List<Vector3> points = new List<Vector3>();
    public List<GameObject> gameObjects = new List<GameObject>();
    private int count = 0;
    void Start()
    {}
    void Update()
    {
        if(count!=points.Count)
        {
            count = points.Count;
            foreach(GameObject g in gameObjects)
            {
                DestroyImmediate(g);
            }
            gameObjects.Clear();
            int i = 0;
            foreach (Vector3 point in points)
            {
                Debug.Log(++i + "  " + point);
                GameObject tem = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), point, Quaternion.identity);
                tem.transform.parent = gameObject.transform;
                gameObjects.Add(tem);
            }
        }
    }
}

Here is a snap:

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Can you walk us through the sequence of events that leads to these cubes appearing in your scene, starting from a completely empty scene? In particular, call out any steps that cause a serialization/deserialization pass, like saving/opening the scene or entering/exiting play mode. \$\endgroup\$
    – DMGregory
    May 8, 2019 at 16:53
  • \$\begingroup\$ It was Unity's SampleScene with camera and Light, then I created an empty GameObject then created the script. After that, set value to 2,3,1 \$\endgroup\$ May 8, 2019 at 17:13
  • \$\begingroup\$ And there is exactly 2+3+1=6 cubes and created sequentially, why is this happening ? \$\endgroup\$ May 8, 2019 at 17:13

1 Answer 1

1
\$\begingroup\$

Silly mistake :

            GameObject tem = GameObject.CreatePrimitive(PrimitiveType.Cube);
            tem.transform.position = point;

added replacing

GameObject tem = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), point, Quaternion.identity);

2 Cubes were created on that line every time.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .