- Home /
Public Variable Help!
In my game, I'm trying to make a wave spawner, so that when every enemy dies it will start a new wave. Right now, either the death line or the variable itself is broken. Here's my monster death system:
var Health : float = 100;
var MaxHealth : float = 100;
var Manager : GameObject;
function OnTriggerEnter(hit : Collider){
if (hit.CompareTag("EditorOnly")){
Health = Health - 1;
}
}
function Update(){
if (Health <= 0){
Manager.livingCreatures = Manager.livingCreatures - 1;
Destroy(gameObject);
}
}
And my Wave script (I modified it from an existing one):
var totalCreatures : int = 2;
static var livingCreatures : int = 0;
var waitToWave : float = 10;
var waveTime : float;
var yourCreature : Transform;
var CurrentWave : float = 1;
var text : GUIText;
function Update() {
text.text = CurrentWave.ToString();
if (livingCreatures == 0) {
if (waitToWave <= waveTime) {
waveTime = 0;
livesCreatures = totalCreatures;
for (var i = 0; i > totalCreatures; i++)
Instantiate (yourCreature, Vector3(0,0,0), Quaternion.identity);
totalCreatures = totalCreatures * 2;
CurrentWave = CurrentWave + 1;
}else {
waveTime += Time.DeltaTime;
}
}
}
It's getting an error that says "NullReferenceEception: Object reference not set to an instance of an object." Please help!
Which line is indicated in the error message? $$anonymous$$ake sure you have set something for each variale in the inspector. Have you set the $$anonymous$$anager
, yourCreature
, text
and waveTime
variables?
Without telling in which line the error is, we can only do guesswork.
For example, my guess is that you forgot to initialize your GUIText in the Editor.
It doesn't say a line number (i.e. (23, 26)). It says like "Boo.Lang." something...
Since both of your scripts seem to be in JavaScript ins$$anonymous$$do of Boo, the error might relate to something completely else. Can you post the whole error text? And are you sure the error is even in one of these files?
again, did you check that you have defined all the public variables in the inspector?
Answer by Smireles · Jun 09, 2013 at 11:26 PM
I noticed a possible error that may be the cause of your code issue.
In your monster code at line 14:
Manager.livingCreatures = Manager.livingCreatures - 1;
It should be:
Manager.GetComponent('WaveSpawn').livingCreatures = Manager.GetComponent('WaveSpawn').livingCreatures - 1;
And probably changing in your WaveSpawn script:
static var livingCreatures : int = 0;
for:
public var livingCreatures : int = 0;
That's it. The rest of the code looks good. At least from here hehe. Try adding some Debug.Logs every here and there so you know exactly how far your code ran before crashing.
Good luck.
Answer by delorean225 · Jun 10, 2013 at 08:24 PM
Solved it! If anyone would like to see the final code, I can show you. Thank you all for helping me, and I know where to go next time I have a problem.
Hey! If a comment or answer helped you solve your problem do not forget to give it a "thumbs up". Only if it helped you.
Glad to hear that you solved your problem. Cheers :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613103838im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player Health and Death 1 Answer
Enemy healthbar script 2 Answers
Enemy AI help 1 Answer