- Home /
Accessing Booleans from other scripts
Hi im trying to get the hang of changing bools and ints from other scripts. in my case im just trying to change the selected to either 1 or 0. The grid highlight script is attached to multiple cubes who are the children of a empty game object named "Pad"
using UnityEngine; using System.Collections;
public class MainGUI : MonoBehaviour {
public GridHighlight script;
// Use this for initialization
void Start ()
{
script = gameObject.GetComponent<GridHighlight>();
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
if(GUI.Button(new Rect(20, 20, 100, 20), "Cube"))
{
script.selected = 0;
}
if(GUI.Button(new Rect(20, 40, 100, 20), "Turret"))
{
script.selected = 1;
}
}
}
Comment
assu$$anonymous$$g "selected" is a public int in the script "GridHighlight", AND that the scripts are both attached to the same object, this should work as written
otherwise, we will need more info...
Are you trying to change the GridHighlight script in your cube? Or is there a GridHighlight script in your pad?
Right now you aren't accessing anything in another object. Your script is getting the GridHighlight script in the same gameObject.(cube)