1
\$\begingroup\$

I'm learning Unity3D and I'm practising accessing game object scripts from other objects. For the most part I've got it down, but I've run into a weird issue causing a NullPointerException.

I've set up a GameObject, with the following script attached to it:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
public Player[] players;

// Use this for initialization
void Start () {
    players [0].currentPosition = 0;
    players [1].currentPosition = 1;
    players [2].currentPosition = 2;
}

// Update is called once per frame
void Update () {
    foreach(Player p in players)
    {
        if(p.mFormationType == Player.FORMATIONTYPE.ATTACK)
            p.transform.position = p.attackPositions[p.currentPosition];
        else
            p.transform.position = p.defensivePositions[p.currentPosition];
    }
}

public void swapPositions(Player selected)
{
    Debug.Log (selected.currentPosition);
    if(selected == null)
        Debug.Log("Balls - " + selected.gameObject);

    if(selected.currentPosition != 0)
    {
        int previousPos = selected.currentPosition;

        players[0].GetComponent<Player>().currentPosition = previousPos;
        selected.currentPosition = 0;
    }
}
}

And I've set the Player objects within the UI. I then later attempt to do this when I click on one of the Player objects:

void OnMouseOver()
{   
    if(this.currentPosition == 0)
        return;

    if(Input.GetMouseButtonDown(0))
    {
        PlayerController pc = this.controller.GetComponent<PlayerController> ();
        pc.swapPositions (this);
    }
}

But it results in an NPE within the swapPositions method in the PlayerController class, when it attempts to access players[...]

I've stepped through and at the start of the PlayerController script they're all there and set up perfectly, but later on, they're null.

Can anyone explain why this is happening please?

Thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ what is this.controller ? are you sure that the game object is pointing to has the PlayerController component ? \$\endgroup\$ – Raxvan Jan 4 '15 at 23:02
  • \$\begingroup\$ The "this.conteoller" is my GameController gameobject with the PlayerController script attached. And within that I've manually dragged in the player objects. \$\endgroup\$ – fantasitcalbeastly Jan 5 '15 at 7:20
  • \$\begingroup\$ Can you please show where you instantiate / initialize / use the controller you are trying to use? Also can you confirm controller inherits from MonoBehaviour? Thanks. \$\endgroup\$ – lvictorino Jan 5 '15 at 7:42
  • \$\begingroup\$ Hi. The controller is set as a public field within the parent Player class and specified manually in the UI (dragged the script into the field). Which can then be referenced by the child player objects. And yes, it is MonoBehaviour as you can see above. I can post a screenshot of the UI, if that helps folk understand? \$\endgroup\$ – fantasitcalbeastly Jan 5 '15 at 8:04
0
\$\begingroup\$

I think instead of players[0].GetComponent().currentPosition, you should just use it players[0].currentPosition

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer, but I had that originally. Same thing happens. By the time I need to call swapPositions, the Player object array only contains nulls. :( \$\endgroup\$ – fantasitcalbeastly Jan 5 '15 at 7:21
  • \$\begingroup\$ I think you also forgot to initialize: void Start () { players = new Player[20]; players [0].currentPosition = 0; players [1].currentPosition = 1; players [2].currentPosition = 2; } \$\endgroup\$ – Poh Jan 6 '15 at 0:57
  • \$\begingroup\$ Thanks. But I was under the impression that I didn't have to initialise if I'd set the game object reference within the UI? All prefabs are present in the scene. Is this not the case? \$\endgroup\$ – fantasitcalbeastly Jan 7 '15 at 18:58
0
\$\begingroup\$

I don't know if it will help anyone or not, but I managed to get this working. I attached my controller script to the main camera and now access the script like so:

Camera.main.GetComponent<PlayerController>().swapPositions(this);

...from within my Player object scripts.

Many thanks for all the help!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.