x


How to pass a component as parameter

I'm getting the following error:

"NullReferenceException: Object reference not set to an instance of an object Ai_Controller.AiInitHeading (.Player_Controller ai, System.String dir, System.String deg) (at Assets/Scripts/Ai_Controller.cs:27) Input_Controller.InputEnter () (at Assets/Scripts/Input_Controller.cs:468) Input_Controller.Update () (at Assets/Scripts/Input_Controller.cs:174)"

Basically, I declare a variable and load it with a C# script component (Player_Controller.)

I then pass that component to Ai_Controller.AiInitHeading() As you can see, the object the Player_Controller component is attached to doesn't seem to make the transition to the function.

Is there some step to passing a script component in a function that I'm overlooking?

AiInitHeading declaration looks like: AiInitHeading( Player_Controller ai, string dir, string deg )

more ▼

asked Nov 09 '11 at 10:17 PM

Sinaz20 gravatar image

Sinaz20
1 9 10 12

Can you post the whole function?

Nov 09 '11 at 10:48 PM DaveA

And can you look up the line 27 where the error occurs because we can't count the lines if we don't know where to start. There's nothing special you have to take care of. A reference to an object behaves like every other value. Just pass it to the function.

If you can't find the error start debugging. Just use Debug.Log and print all parameters you pass to the function to see if it's null. If it's null you have to go up the chain and find out why it's null. If it's not null you have to go down the chain.

Nov 10 '11 at 12:01 AM Bunny83

This function is declared inside a class Ai_Controller which inherits from Monobehaviour but is not added to any object as a component.

public static void AiInitHeading( Player_Controller ai, string dir, string deg ){

    ai.QueOrdersHeading.Enqueue( "heading" );

    //load turn direction
    switch( dir ){
        case "to port.":
        case "abeam.":
         ai.QueOrdersHeading.Enqueue( "1.0" );
            break;
        case "to starboard.":
         ai.QueOrdersHeading.Enqueue( "-1.0" );
            break;
        case "galactic.":
         ai.QueOrdersHeading.Enqueue( "0.0" );
            break;
    }

    ai.QueOrdersHeading.Enqueue( deg );

}

It get's called like so from some object's component script:

Ai_Controller.AiInitHeading( myPlayer, stackInput.Pop(), stackInput.Pop() );

Where mPlayer is declared as:

public Player_Controller myPlayer;

With other member variables and initialized in Start() as:

myPlayer = GetComponent< Player_Controller >();
Nov 10 '11 at 12:08 AM Sinaz20

I see two possible reasons:

  • GetComponent< Player_Controller >(); returns null because there is no such script attached to this GameObject.
  • The QueOrdersHeading variable inside your Player_Controller script has not been initialized.
Nov 10 '11 at 12:11 AM Bunny83

It turns out when I dragged the Player_Controller component onto the object-- I missed :P But in such a way that I was happily looking at the wrong object going "the component is right THERE, you stupid console!"

Nov 10 '11 at 01:17 AM Sinaz20
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x478
x303
x38

asked: Nov 09 '11 at 10:17 PM

Seen: 788 times

Last Updated: Nov 10 '11 at 01:17 AM