- Home /
When using an Array i get a NullReferenceException
So i'm currently looking to implement a spawning script for my character which amongst other things will turn off his visibilty upon entering a 'death' trigger box and make him reappear when he respawns.
The problem i'm having is that my character is in several parts and so to access the Renderer in all parts i used a bulletin array and populated with my character's mesh's.
This is the script:
var spawnpointdeath : Transform;
var character : Renderer[];
function OnTriggerEnter(other : Collider) {
if (other.tag == "Player") {
character.renderer.enabled = false;
yield WaitForSeconds (.5);
other.transform.position = spawnpointdeath.position;
character.renderer.enabled = true;
}
}
When i run the game i get the error 'NullReferenceException: Object reference not set to an instance of an object'.
If i don't use an array and just add one element of my characters mesh to a variable it works fine and obviously with the error message all things point to my array but i'm not sure whats going on. Do i need to make instances of my characters meshes to make it work? I don't understand why it would work as a variable with one mesh and not an array with several meshes.
As you might have guessed, I'm new to Unity and to scripting and have for the last couple of days been searching and trying to work out what i'm doing wrong but can't seem to find the answer. Any help would be greatly appreciated :)
The error message itself though comes from not assigning objects to the array. Do this for every position of the array in the inspector.
If you've done this, and it still doesn't work, try this:
Since character is an array I think you have to specify position, for example: character[0].renderer.enabled = false;
So if you want to set the whole array to false you need to iterate through each position: for (var i = 0; i < character.length; i++) { character[i].renderer.enabled = false; }
Didn't realise i had to make it apply to each entry (one by one or through iteration), i thought by default it would just apply it to the whole array.
This worked a treat, thanks Dreamish! Though i can't make your response an answer, i think you'd have to repost.
Answer by cvgwegwegwe · Aug 27, 2011 at 12:34 PM
ONLINE STORE :
====( http://popbuynow.com/ )=====
The website wholesale for many kinds of fashion shoes, like the nike,jordan,prada,, also including the jeans,shirts,bags,hat and the decorations. All the products are free shipping, and the the price is competitive, and also can accept the paypal payment.,after the payment, can ship within short time.
free shipping
competitive price
any size available
accept the paypal
jordan shoes $32
nike shox $32
Christan Audigier bikini $23
Ed Hardy Bikini $23
Smful short_t-shirt_woman $15
ed hardy short_tank_woman $16
Sandal $32
christian louboutin $80
Sunglass $15
COACH_Necklace $27
handbag $33
AF tank woman $17
puma slipper woman $30
====( http://popbuynow.com/ )=====
Answer by cvgwegwegwe · Aug 27, 2011 at 12:37 PM
ONLINE STORE :
====( http://popbuynow.com/ )=====
The website wholesale for many kinds of fashion shoes, like the nike,jordan,prada,, also including the jeans,shirts,bags,hat and the decorations. All the products are free shipping, and the the price is competitive, and also can accept the paypal payment.,after the payment, can ship within short time.
free shipping
competitive price
any size available
accept the paypal
jordan shoes $32
nike shox $32
Christan Audigier bikini $23
Ed Hardy Bikini $23
Smful short_t-shirt_woman $15
ed hardy short_tank_woman $16
Sandal $32
christian louboutin $80
Sunglass $15
COACH_Necklace $27
handbag $33
AF tank woman $17
puma slipper woman $30
====( http://popbuynow.com/ )=====