Answer the question
In order to leave comments, you need to log in
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";
}
}
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
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 questionAsk a Question
731 491 924 answers to any question