GetComponent problem

I’m trying to make a game where there are robots moving in the streets. So I have done some hidden spheres (called A, B, C, D, E…) in the scene and put them in the end of the streets. Little bit like Pacman.

The robot starts from the point of sphere A and then it moves to the point of sphere B. It works ok.

When the robot reaches the position of sphere B it should then randomly give the Robot new shere to move to. That sphere itself has some manually given alternatives. Meaning alternative sphere-gameobjects that I’ve dragged to the inspector to use as their next position.

Now the problem is:

In each of the sphere there is a “OnTriggerEnter” function that waits for the robot to enter.

function OnTriggerEnter ( other : Collider ) {
	Debug.Log("One Robot has arrived here, now lets give it a new sphere to go to");
	if (other.GetComponent(RobotAI))
	other.GetComponent(RobotAI).FindAnotherSphereToGoTo();
}

So the sphere shoud pass information of next destination to the robot. At first I’m just trying to acces the robot via sphere, but now I’m stuck because I can’t acces Robot’s function to begin with. For some reason the script FindAnotherSphereToGoTo() here is not activated with this OnTriggerEnter code. It says the Debug.Log note “One Robot has arrived…”, but function does nothing.

If I remove the “if (other.GetComponent(RobotAI))” it says: “NullReferenceException: Object reference not set to an instance of an object”

Im just glancing over you code but what i think could be wrong is that, you are assuming that the collider is a gameObject. A collider and a gameObject is not that same thing. Ah collider CONTAINS a gameObject, namely the object that has collided.

function OnTriggerEnter ( other : Collider ) {
	Debug.Log("One Robot has arrived here, now lets give it a new sphere to go to");
	if (other.gameObject.name("nameOfYourObject")){
	    other.gameObject.getComponent(RobotAI).FindAnotherSphereToGoTo();
        }
}

and i think if im correct, that you can’t call the method on the component without getting it first. But i could be wrong here.

Remember to next time that you can always do a debug on whatever object and see what you get. If you do;

Debug.Log(other.GetComponent(RobotAI));

it will probably give you “null” and that should tell you that your method is not working.

You were right. It gave me “null”.

Then I noticed I had gameobject with the RobotAI script, and a child for that gameobject with capsule collider “Is Trigger” set on. So the collider and the script were in different places and it gave null. I didn’t understand this until you advised me to debug the name.

So I fixed that. Now the script and the collider are on the same gameobject and the debug found the “other.gameObject.name” that is “Capsule” insted of null.

Then I tried your code, but it gave me error “It is not possible to invoke an expression of type ‘String’.”

    function OnTriggerEnter ( other : Collider ) {
        Debug.Log("One Robot has arrived here, now lets give it a new sphere to go to");
        if (other.gameObject.name("Capsule")){
            other.gameObject.getComponent(RobotAI).FindAnotherSphereToGoTo();
            }
    }

Don’t know why. But now I got it working without the if-statement.

function OnTriggerEnter ( other : Collider ) {
	Debug.Log("One Robot has arrived here, now lets give it a new sphere to go to");
	Debug.Log(other.gameObject.name);
	other.gameObject.GetComponent(RobotAI).FindAnotherSphereToGoTo();
}

Thanks for all the help! I was lost for hours with this one. :slight_smile:

If they are on the same GameObject then you can skip the other.gameObject part because Collider has GetComponent exposed to it. “other.gameObject” would still fail if they were on separate objects in the hierarchy. other.GetComponent(RobotAI) will work just fine with your new setup.

That being said - it would still be a good idea to check for its existence beforehand.

RobotAI ai = other.GetComponent<RobotAI>();
if (ai != null)
{
    ai.FindAnotherSphereToGoTo();
}

Sorry my bad name is actually an variable and not a function.

So name == “nameOfYourObject”

And the if-statement is good to have, cause lets say you have multiple objects colliding. You have to be able to sort out wich object you want to trigger, and what to trigger.