Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

Object reference not set to an instance of an object

$
0
0
Hello, I'm pretty new to Unity and Java programming and I've been trying to make a score system. I have a script called ScoreController and it looks like this : private var textScore : GUIText; public var Score : int; var Amount : int; function Awake() { textScore = GameObject.Find("TextScore").GetComponent(GUIText); } function Update() { textScore.text = "Score: " + Score.ToString(); } function AddScore() { Score++; } function SubtractScore() { Score--; } function SetScore(Amount) { Score = Amount; } And I'm using enemy object with script CountdownTimer : var scoreController : ScoreController; public var allowedTime : int = 5; private var currentTime = allowedTime; function Awake() { TimerTick(); } function TimerTick() { while(currentTime > 0) { yield WaitForSeconds(1); currentTime--; } scoreController.GetComponent(ScoreController).SubtractScore(); Destroy (this.gameObject); } I want it to wait 5 seconds, subtract score and destroy itself. I also have other script that I found on google. It instantiates objects randomly so they all become clones of a prefab. var spawnPoints : Transform[]; // Array of spawn points to be used. var enemyPrefabs : GameObject[]; // Array of different Enemies that are used. var soundSpawn : AudioClip; var amountEnemies = 20; // Total number of enemies to spawn. var yieldTimeMin = 0.5; // Minimum amount of time before spawning enemies randomly. var yieldTimeMax = 1.5; // Don't exceed this amount of time between spawning enemies randomly. var i = 5; function Start() { Spawn(); } function Spawn() { for (i=0; i<10; i++) // How many enemies to instantiate total. { yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated. var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate. var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next. audio.PlayOneShot(soundSpawn); Instantiate(obj, pos.position, Quaternion.Euler(75, 0, 0)); i=0; } } But whenever I run the game and 5 seconds pass I get the following error and the object doesn't destroy itself **Object reference not set to an instance of an object** This error happens because of this line: scoreController.GetComponent(ScoreController).SubtractScore(); I've been looking for how to correct it all over the place, however I was unable to solve it myself so I'm asking help from you guys. Please help.

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>