I’m having issues instantiating a prefab object. It was working fine, then I changed the sprite and name in the code and now the only way it will work is if the object that is supposed to instantiate is already in the scene. Here is the code I’m using:
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D> ();
myCollider = GetComponent<Collider2D> ();
InvokeRepeating ("Spawn", spawnTime, spawnTime);
theGround = GameObject.Find ("ground");
}
void Spawn () {
Vector3 topRight = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, transform.position.z));
bottomRight = theGround.transform.position.y;
Instantiate (GameObject.Find ("goal"), new Vector3 (5f, Random.Range (bottomRight + 0.5f, topRight.y - 0.5f), 0), Quaternion.identity);
}