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\$
4
  • \$\begingroup\$ what is this.controller ? are you sure that the game object is pointing to has the PlayerController component ? \$\endgroup\$
    – Raxvan
    Jan 4, 2015 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\$ Jan 5, 2015 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, 2015 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\$ Jan 5, 2015 at 8:04

2 Answers 2

0
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\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\$ Jan 5, 2015 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, 2015 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\$ Jan 7, 2015 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\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .