Hi!

I have this piece of code. It’s choosing a random prefab out of a array and find a script of its own type in it, which I earlier attached to the prefab. The problem now is that when I run this “newCross” is whyever referenced to itself and I have no plan why

	public void SpawnNextCrossroad(){
		for (int i = 0; i < hookPoints.Length; i++) {
			if (hookPoints*.name != hookUsed) {*
  •  		GameObject newCrossObj = crossroads[Mathf.RoundToInt(Random.Range(0, crossroads.Length - 1))];*
    
  •  		Crossroad newCross = newCrossObj.GetComponent<Crossroad> ();*
    
  •  		int distance = Mathf.RoundToInt(Random.Range (minDistanceBetweenCroasroads, maxDistanceBetweenCrossroads));*
    

Vector3 posFac = hookPoints .position - calcPoints .position;
* newCross.SpawnThis (posFac);*
_ Instantiate (newCrossObj, (posFac * distance) + transform.position, Quaternion.identity);
* }
}*_

Here a small sketch of my problem:
[83349-zeichnung1.jpg|83349]

that’s because you’re not doing what you describe in your sketches. you get the prefab, get ITS component and then instantiate it. You should do the GetComponent on the returned gameobject from the instantiate. BTW.

crossroads[Random.Range(0, crossroads.Length)];

suffices, because Range here returns an int and excludes the last possible value