Jump to content

So i am making a pause menu and i need it so when i click the continue button in the pause menu that it turns the "isEnabled" variable in another script to false.

 

When i make add in the code i researched for accessing another script, I have been getting an error.. 

 

Here is the Error:

I keep getting an error like this
"Assets/WaterBottle.cs(12,42): error CS0120: An object reference is required to access non-static member `SurvivalSystem.drink'
"

 

Here is the code that runs the continue button:

using UnityEngine;
using System.Collections;public class PauseMenu : MonoBehaviour {
  public PauseController disable; //Variable for making a copy of the script.
    public AudioSource ClickSound;
    public GameObject pauseToggle;
    public void start()
    {
        disable = GameObject.FindGameObjectWithTag("Player").GetComponent<PauseController>();//code to access the other script from the player controller.
    }
    public void Continue()
    {
        disable.isEnabled=(false); //To disable the pause menu in the other script.
        pauseToggle.SetActive (false); //Ignore
        Debug.Log ("Continuing game"); //Debug log
    }
//YOU CAN IGNORE EVERYTHING UNDER THIS COMMENT LINE!!!!!!!
    public void Home()
    {
        Debug.Log ("Loading Main Menu");
        pauseToggle.SetActive (false);
        Application.LoadLevel ();
    }
    public void QuitToDeskTop()
    {
        ClickSound.Play ();
        Application.Quit ();
    }
}

And here is the code that i am using to enable and disable my pause menu:

using UnityEngine;
using System.Collections;

public class PauseController : MonoBehaviour {
	
	public GameObject pauseMenu; //To enable the pausemenu overlay.
	public bool isEnabled = false; //Disable pause menu on start up.

	public void start()
	{

	}
	
	void Update()
	{
		// Enable pause menu
		if (Input.GetKeyDown(KeyCode.Escape) && !isEnabled)
		{
			pauseMenu.SetActive(true);
			isEnabled = true;
		}
		
		// disable pause menu
		else if (Input.GetKeyDown(KeyCode.Escape) && isEnabled)
		{
			pauseMenu.SetActive(false);
			isEnabled = false;
		}
	}
	
}

Can somebody please help me! I need this for a project in college and I want to get it done over the weekend! I will really appreciate it if someone can help me get these two scripts to work with each other.

 

Thanks for reading!

 

-Scott(SD2020_)

Link to post
Share on other sites

In Unity, GameObjects have many and different components. User-made scripts are among them. Having a reference to a GameObject, you can easily access each component using the GetComponent<Type>() function.

For example, let's say you wanted to access another script from another GameObject. Let the script be called Script.

You get a reference to the GameObject itself.

public GameObject o;

Then to get the Script component from the GameObject :

private Script script = o.GetComponent<Script>();

 

This works with all components. For example, to get the BoxCollider from another GameObject :

private BoxCollider bCol = object.GetComponent<BoxCollider>();

 

And if you want to access a component of the parent GameObject you can simply call the function. Or call it with "this", whatever.

private AnotherScript anotherScript = GetComponent<AnotherScript>();

This will put a reference to the AnotherScript from the same GameObject as the current script being run.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×