G
G
Gvinblyad13372021-10-22 01:54:59
Unity
Gvinblyad1337, 2021-10-22 01:54:59

Why doesn't the script handle object clones?

Actually, the script for one object works correctly. However, in the scene I have the generation of 10 such objects (that is, clones) and the script does not act on them (that is, it does not change the color of the prefab element and its tag to green). How to fix it?

public class RandomColorAndTag : MonoBehaviour
{

    System.Random randomElement = new System.Random();
    int el = 0 ;
  
    void Start()
    {

        el = randomElement.Next(2, 6);

             GameObject.Find($"Cube ({el})").GetComponent<Renderer>().material.color = Color.green;

            if (GameObject.Find($"Cube ({el})").GetComponent<Renderer>().material.color == Color.green)
            {
             GameObject.Find($"Cube ({el})").transform.tag = "Green";        
    }
}


Here is the GameManager script

public class GameManager : MonoBehaviour
{
    public GameObject firstBarrier;

    public GameObject secondBarrier;

    public int numOfBarriers = 0;

    System.Random rnd = new System.Random();

    System.Random rndY = new System.Random();

    Vector3 vector = new Vector3(5, 1/3 , 1);
    void Start()
    {
        for (numOfBarriers = 0; numOfBarriers < 10; numOfBarriers++)
        {
            int ewq = rnd.Next(1, 20);

            int rY = rndY.Next(1, 4);
           
            if(ewq <= 10)
            {
              Instantiate(firstBarrier, vector, Quaternion.identity);
            }
            else
            {
                Instantiate(secondBarrier, vector, Quaternion.identity);
            }

            vector.x -= 7;

            vector.y = rY;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Milevsky, 2021-11-13
@Hackerman1

You should also hang a script on them automatically, in theory. I don’t know for sure, but it seems impossible to hang up a script when creating an object through another script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question