GameObject only spawning 60% of the time (c#)
I am currently working on my first unity game outside of a tutorial. I am trying to get a coin to spawn on the y axis at random positions. I have done this in c#, except the code only works some times (60% - 70% of the time). The other times the coin does not spawn and an error message saying NullReferenceException: Object reference not set to an instance of an object appears. Can you please help me.
Here is the scripts thats spawns the coin prefab:
using UnityEngine;
using System.Collections;
public class CoinSpawner : MonoBehaviour {
public GameObject Collectible;
private float Miny = -2.6f;
private float Maxy = 2.6f;
// Use this for initialization
void Start () {
Debug.Log ("Called");
spawnCoin ();
}
public void spawnCoin () {
Vector3 coin = new Vector3 (-2.1f, Random.Range (Miny, Maxy),0);
Instantiate (Collectible, coin, Quaternion.identity);
}
// Update is called once per frame
void Update () {
}
}
Here is the script that tells the coin what to do once spawned:
using UnityEngine;
using System.Collections;
public class CoinBehaviour : MonoBehaviour {
public CoinSpawner coinSpawner;
public int scoreValue = 1;
public ScoreKeeper scoreKeeper;
// Use this for initialization
void Start () {
// Get Score text thingy
scoreKeeper = GameObject.Find ("0000").GetComponent<ScoreKeeper> ();
// Get coin spawner thingy
coinSpawner = GameObject.Find ("Spawner").GetComponent<CoinSpawner>();
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "Player") {
Debug.Log ("Hit");
Destroy (gameObject);
coinSpawner.spawnCoin();
scoreKeeper.Score(scoreValue);
}
}
}
Picture of Game while working:
Picture of Game while not working:
Are you sure you have got dragged a coin prefab onto the script so there is an object to instantiate?
Yes. The code works most of the time but not all the time.
Answer by outasync · Oct 23, 2015 at 09:58 PM
I could be mistaken but change this:
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "Player") {
Debug.Log ("Hit");
Destroy (gameObject);
coinSpawner.spawnCoin();
scoreKeeper.Score(scoreValue);
}
to this
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "Player") {
Debug.Log ("Hit");
coinSpawner.spawnCoin();
scoreKeeper.Score(scoreValue);
Destroy (gameObject);
}
It look like you are deleting the object before calling your spawn, which I think would create the problem you are getting
THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!!!!!. That worked. Could you explain to me why.
@HumanBeast Simple really, you were destroying the object that is trying to call coinSpawner.spawnCoin()
, sometimes it would work because of the way Unity handles time.
I would like to expand on the reason it happens that way. I believe the best reason is it's most likely procedural. The order of events you wish to happen went from: "Detroy" ==> "$$anonymous$$ake this happen". You shouldn't have an object try to do something after it's been destroyed or you'll receive messages like that. Ins$$anonymous$$d, make sure to have it flow like: "$$anonymous$$ake this happen" ==> "It's ok to die now". Hope that clears it up a bit.
Your answer
Follow this Question
Related Questions
C# unexpected symbol error 1 Answer
Null Reference Exception Error 1 Answer
error CS1525: Unexpected symbol `float' 1 Answer
Struggling with Unity 5 GetComponent: Null reference 0 Answers
Cascading dropdown menus 0 Answers