I’m doing the Walker Boys Studio lab 2, part 33. But I’m writing all the scripts in C# to get that sweet auto-complete goodness.
The issue I’m running into occurs when the asteroid hits the player’s shield. Pretty sure they’re both set up right (a sphere with a sphere collider, IsTrigger=true, and a rigidbody).
Here’s the code that runs for the asteroid:
void OnTriggerEnter(Collider other)
{
if (other.tag == "shield")
{
HitShield(other.gameObject);
ExplodeAsteroid();
ResetPosition();
}
}
void HitShield(GameObject other)
{
scriptShield localShield = (scriptShield)other.GetComponent(typeof(scriptShield));
localShield.HitShield();
}
The error I’m getting is NullReferenceException: Object reference not set to an instance of an object, which occurs when I try to call localShield.HitShield();. Does it matter that the shield object is a prefab which is instantiated by the player prefab?
How can the object be null if I can read its tags? There’s something fundamental I’m missing, right?
Sorry forgot to say, I know you wanted to do this with ship thrusters so what you do is read the final torqueVector. Figure out which thruster is firing on the x axis and feed torqueVector.x to it, y thruster torquevector.y, z thruster torqueVector.z. Obviously you'll need to write some code to make this work but thats the logic and you should be able to follow it on your own. And it will be good practice since your getting into Newtonian stuff, this is like 101. You can do it though!
– SpaceManDan