1

I'm having an issue where I can't disable a script from the other script - they are both public and within the same package (I think).

Here is my code for the script I'm trying to disable from:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
using RTS;

public class PauseMenu : MonoBehaviour {

Canvas canvas;
private Player player;
public Button Button2;


void Start()
{
    Debug.Log ("asdf");
    player = transform.root.GetComponent< Player >();
    canvas = GetComponent<Canvas>();
    canvas.enabled = false;
    ResourceManager.MenuOpen = false;
    Button2.GetComponent<Button>().onClick.AddListener(() => { Resume();});
    if(player) player.GetComponent< UserInput >().enabled = false;
}

And the code for the other script:

//sets up what resources we are using
using UnityEngine;
using System.Collections;
using RTS;

public class UserInput : MonoBehaviour {

//sets up a private variable for only this class - our player
private Player player;

// Use this for initialization
void Start () {
//this goes to the root of the player ie the object player and allows us to
player = transform.root.GetComponent< Player > ();
}//end Start()

So the part that is not working is:

if(player) player.GetComponent< UserInput >().enabled = false;

And the code runs and then causes the runtime error:

NullReferenceException: Object reference not set to an instance of an object
PauseMenu.Pause () (at Assets/Menu/PauseMenu.cs:40)
PauseMenu.Update () (at Assets/Menu/PauseMenu.cs:29)

Here is a picture showing my scene hierarchy and components: Scene hierarchy

3
  • An alternative possibility to what Jorge suggests is that player.GetComponent< UserInput >() is returning null. In any case, try logging out the returned values from GetComponent() to narrow down the reason for your problem. Commented Oct 15, 2015 at 15:19
  • Thanks - it was the player bit going wrong as the userinput is part of the player object - i've posted a screenshot showing hierarchy but im not sure how to access the scripts within this object from my pausemenu Commented Oct 15, 2015 at 19:33
  • To clarify, which object in your scene contains the PauseMenu script as a component? Commented Oct 15, 2015 at 19:39

2 Answers 2

1

The issue here is that you try to execute transform.root.GetComponent< Player >(); from within PauseMenu, which is on the "Canvas" object.

The problem with that is that the topmost transform in the hierarchy of your "Canvas" object (which is what transform.root returns) is, well, the transform of the "Canvas" object - which is in no way related to the UserInput script you are trying to access. For this script to actually work, you would need the transform of your "Player" object, which is the object that actually has the UserInput script.

My suggestion is to eliminate the need to run GetComponent() at all - create a public UserInput variable in your PauseMenu class, then (while selecting your "Canvas") in the editor, drag the "Player" object into that new field. This will cause the UserInput script of your "Player" object to be accessible within the PauseMenu.

So your PauseMenu script might look like:

public class PauseMenu : MonoBehaviour {

    Canvas canvas;
    public UserInput playerInput; // Drag the Player object into this field in the editor
    public Button Button2;

    void Start()
    {
        Debug.Log ("asdf");
        canvas = GetComponent<Canvas>();
        canvas.enabled = false;
        ResourceManager.MenuOpen = false;
        Button2.GetComponent<Button>().onClick.AddListener(() => { Resume();});
        playerInput.enabled = false;
    }
}

Hope this helps! Let me know if you have any questions.

(An alternative is to use GameObject.Find("Player") to get GameObject of "Player". This needs a bit more code but doesn't use the editor.)

Sign up to request clarification or add additional context in comments.

2 Comments

Great this works perfectly :) will there be any side effects with giving the pausemenu access to the player and having it as a public variable?
Glad I could help! I can't think of any problems that may arise, and it's the first way the Unity manual suggests (see Accessing Other Objects). I've found it really useful, hopefully this opens a lot of doors for you too.
1

I would say your player = transform.root.GetComponent< Player >(); arrives null. So you are trying to disable something that doesnt exist. Enter debug mode and see if your player is null or not.

1 Comment

Thanks your right - it returns null - i realized it's because the script is not attached to the player but i don't want to do that - any way to access player from this script? I will attach a screenshot showing you the hierarchy and scripts for player (once i figure how :P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.