Search Unity

Instanciate bug?

Discussion in 'Scripting' started by Nossgrr, Jul 7, 2018.

  1. Nossgrr

    Nossgrr

    Joined:
    Dec 18, 2005
    Posts:
    34
    Hey guys,

    Simple problem, create a sphere and attach a script to it with the following code..
    As you see, I'm trying to instantiate "One" ball on the Start routine.. What ends up happening is infinite ball spawning.. Really odd, that should only instantiate one ball no?
    This happens in 2018.1 and .2

    Code (CSharp):
    1. public class BallScript : MonoBehaviour {
    2.  
    3.     private GameObject Ball;
    4.  
    5.     // Use this for initialization
    6.     void Start()
    7.     {
    8.         Ball = GameObject.Find("Sphere");      
    9.         Instantiate(Ball, new Vector3(0, 1, 0), Quaternion.identity);      
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.     }
    16. }
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    If BallScript is on your ball then your BallScript is spawning a ball, which has a BallScript, which is spawning a ball, which has a BallScript, which is spawning a ball...
     
  3. Nossgrr

    Nossgrr

    Joined:
    Dec 18, 2005
    Posts:
    34
    Ahh yes.. Good catch MadgVox, ty!