Hey guys,
This is an interesting one… Now I know what the ‘object reference not set error means’ however I’m not sure why it’s coming up here.
Basically I’m wanting to turn off the player’s collider whilst it’s in an elevator (using 2D physics) so it doesn’t causes issues with passerbys, and then turn it on when the player gets off. here’s my offending code…
// turn off all the colliders whilst on the elevator
Collider2D[] allColliders2D = GetComponents<Collider2D>();
Debug.Log ("There are " + allColliders2D.Length + " colliders in object " + transform.name);
for (int i = 0; i < allColliders2D.Length; i++)
{
Debug.Log ("i = " + i);
Debug.Log ("Collider is on object " + allColliders2D [i].transform.name + " with a phyics material of "+ allColliders2D[i].sharedMaterial.name);
if (allColliders2D [i].sharedMaterial.name == "Player")
{
Debug.Log ("Turning off a collider on player " + playerNumber + " and i = " + i);
allColliders2D [i].enabled = false;
}
}
I am getting the following errors:
The funny thing is that I have 2 colliders on the player object (one set to trigger, the other the ground collider). So why is it tripping when it’s trying to check the second one. I changed the for loop to " … i < allColliders.length - 1…" and this stopped the tripping… but if in the future I want to run through several colliders on an object I’m going to need to know what I did wrong.
If the second collider is set to a trigger would this cause this error?
No, but you’d have a problem if the second collider doesn’t have a material. Possibly that would be the case for a trigger? I’m not really sure, but it’s something to investigate.
True, but it didn’t seem to even get that far, as it error’d at the debug line that would print the name just before checking the material.
Oh well, I’ll have to set up some test scripts and post the results - unlikely it’s a bug, but you never know.
Thanks for the help JoeStrout! XD
No, that line does check the material. And since you didn’t quote the output, that’s why I suspected that line.
[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Debug']Debug[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Log']Log[/URL] ("Collider is on object " + allColliders2D [i].transform.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=name']name[/URL] + " with a phyics material of "+ allColliders2D[i].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=sharedMaterial']sharedMaterial[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=name']name[/URL]);
See, right there, we’re accessing allColliders2D.sharedMaterial.name. That will throw an exception if allColliders2D.sharedMaterial is null.
Best,
P.S. This might be a good opportunity to learn to use the debugger. With that, it will stop right where the problem is, and let you examine objects by direct inspection or by typing an expression into the “immediate” window — often a much faster way to see what’s going on.
Thanks again @JoeStrout
Yes - you’re absolutely right… I forgot that part was there (suffering with brain fog…), my apologies.
Yes I’m just starting to learn how to use the debugger (attaching monodevelop to a unity process, etc.) so will keep looking into that.
Again, sorry for being a muppet and disagreeing with you, lol 
Just to finalise…
I adjusted the loop as follows:
void SetColliders(CircleCollider2D[] colliders, bool state)
{
for (int i = 0; i < colliders.Length ; i++)
{
// New line here!!!
if (colliders [i].sharedMaterial)
{
if (colliders [i].sharedMaterial.name == "Player")
{
Debug.Log ("Turning off a collider on player " + playerNumber);
colliders [i].enabled = true;
}
}
}
}
Basically, it will only try to get the name IF there is a material in the first place. If there’s no material it won’t TRY to get the name, and therefore won’t error
Again, thanks for the help @JoeStrout
1 Like
Note that you can combine the two if-statements with “&&” (and). The && operator does “short-circuit evaluation” which means that if the first part is false, it doesn’t even try the second part. So:
void SetColliders(CircleCollider2D[] colliders, bool state) {
for (int i = 0; i < colliders.Length ; i++) {
// New line here!!!
if (colliders [i].sharedMaterial && colliders [i].sharedMaterial.name == "Player") {
Debug.Log ("Turning off a collider on player " + playerNumber);
colliders [i].enabled = true;
}
}
}
1 Like
Ah!
See, I kept it seperate because I wasn’t sure if it did them together or not. Now I know!
Many thanks again Joe! XD