Why when setting a tag to gameobjects the first one is untagged ?

void CreateCubesBesideWaypoints()
    {
        const int lightCount = 20;
        Vector3[] lightPositions = new Vector3[lightCount];

        for (int i = 0; i < waypoints.Length - 1; i++)
        {
            posToChunkDistances(waypoints[i].transform.position, waypoints[waypoints.Length - 1].transform.position, lightPositions, lightCount);
            for (int x = 0; x < lightPositions.Length; x++)
            {
                lightPrefab.GetComponent<Renderer>().material.color = Color.red;
                Instantiate(lightPrefab, lightPositions[x], Quaternion.identity);
                lightPrefab.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                lightPrefab.tag = "LightPrefab";
            }
        }
    }

What i want to do is not to clone many times the lightPrefab i want to Instantiate Cube GameObjects to be children under the lightPrefab and give each cube a tag “LightPrefab”.

I should use CreatePrimitive instead Instantiate ?

You are altering the color, scale, and tag of the prefab itself. Not the instance you have created.

Look at the examples in the scripting reference to get a reference to the instance.

1 Like