The code is to pick up an object which increases the players speed.
maxForwardSpeed is a variable of class characterMotorMovement which is in CharacterMotor.
I get nullReferenceException when i run this code and try to pick up the object.
function OnTriggerEnter(other:Collider)
{
var fpc = other.gameObject;
var fpcComponent = fpc.GetComponent("CharacterMotor");
fpcComponent.CharacterMotorMovement.maxForwardSpeed = newSpeed;
Destroy(gameObject);
1- CharacterMotorMovement is the type, not the actual variable; the CharacterMotorMovement variable created in CharacterMotor.js is movement.
2- You can use other.GetComponent(…) directly.
3- GetComponent(“CharacterMotor”) returns type Component, and you need the type CharacterMotor to access the movement variable - pass CharacterMotor without quotes and GetComponent will return the correct type.
4- To avoid runtime errors case other object enters the trigger, check if fpcComponent isn’t null (what happens when the object doesn’t have a CharacterMotor script):
// you can get a component using the 'other' collider directly:
var fpcComponent = other.GetComponent(CharacterMotor); // use the type without quotes
if (fpcComponent){ // check if fpcComponent isn't null
fpcComponent.movement.maxForwardSpeed = newSpeed;
}