accessing external variables

hey guys

I know this is a common question but I can’t for the life of me remember how to do it.

I need to access a boolean from a script in a separate gameobject.

I think I remember doing a ‘find’ for the game object but don’t remember the rest :frowning:

thanks a bunch,
Matt

You can use something like this:

var otherScript = GetComponent(OtherScript);
print(otherScript.isWalking); //isWalking is declared as boolean in the OtherScript

doesn’t seem to work… don’t you have to declare the type of variable the script is?

This is C#

public GameObject menuGO;
public MenuManager menuM; // script

menuGO = GameObject.Find(“GO_Menu”);
menuM = menuGO.GetComponent();

menuM.myBoolean = true;

I get this:
Assets/pausemenu.cs(31,26): error CS0176: Static member `pausebutton.clickedpause’ cannot be accessed with an instance reference, qualify it with a type name instead

here’s a snippet of my code:

public GameObject pausecomp;
public pausebutton menuM; // script

	// Use this for initialization
	void Start(){
		
		
pausecomp = GameObject.Find("pause");
menuM = pausecomp.GetComponent<pausebutton>();


		
	}
	void Update () {
			
		if(menuM.clickedpause = false){
			print("hello");
		}

Well, I missed read your post. You wan to access a variable from other game object not a variable in the same game object. For example, the isWalking is declared in the OtherScript as the following:

//OtherScript.js
var isWalking: bool = false;

function Start()
{
  isWalking = true;
}

function Update()
{
  /* 
   if (Input.GetKeyDown ("w"))
    {
       isWalking = true;
    }
   */
}

in your current game object, you can access the isWalking as the following:

var other : OtherScript = FindObjectOfType(OtherScript);
print(other.IsWalking);

gives me all kinds of overloads and stuff :frowning:

Oh you made it static.

Forget finding things just use it directly. Assuming pausebutton is the class name.

pausebutton.clickedpause=true;

Class names are supposed to be capitalized.
class PauseButton

no errors… looks like it works! :slight_smile:

thank you very much!

cheers,
Matt

hm… is there any reason why even when I have the static variable in my other script set to false, the variable passes as true in the script that finds the other variable? (sorry if that’s a bit confusing lol)

Are you using the PauseButton class in more than one GameObject?

Maybe dont make it static and go back to my first post.

yes, more then one game object.

I have a pause button at the top left of the window and when I click it, I want a little menu to drop down.
the problem is that the drop down menu is one game object and the pause button is another.

…tried it but doesn’t change the boolean to what I set it to =/

If there is only one pause button in the game the script should only be on one GameObject.

Then you can look at it with:

pausebutton.clickedpause

If its not being set correctly, or displayed correctly, or printed to the console correctly, thats just a bug you will have to find or post code.
When debugging its easy to make different mistakes that you dont see until the main problem is fixed.

For some reason my script won’t access other scripts in the same project.
I.e. I have two scripts: Script1 and Script2.
In Script1 I cannot call Script2.variable
Anyone know why?