- Home /
Read a variable from other script of same object.
I tried everything but it just does not work, im at a loss.
Script 1, attached to object Cube1</p> <pre><code>using UnityEngine; using System.Collections; public class clicked : MonoBehaviour { public bool isclicked = false; public int clickedby = 0; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
Script 2, attached to object Cube1
void OnMouseDown()
{
os = GetComponent<clicked>();
os.isclicked = True;
}
Why does this not work ? Its exactly as in the script reference on unity site, however, i get messages about os does not exist in current context.
I hope someone can help. My next problem is that the scripts are both on 9 cubes in my scene, and i would like to read out the isclicked value from another object too ;) but if i can solve my first problem, i think i can do my other one too.
Answer by Mortennobel · Apr 15, 2011 at 07:15 PM
Try this:
clicked os = GetComponent<clicked>();
os.isclicked = True;
You forgot to declare the variable os. (You might have copied the code from a JavaScript example, while writing C# code).
Your answer
Follow this Question
Related Questions
Execution order of multiple scripts attached to different GameObjects 1 Answer
Get a GameObject from a component 1 Answer
GetComponent(Script) Error(Won't detect script) 0 Answers
Destroying a wall after collecting X amount of items? 1 Answer
Making script object without declaring script name 2 Answers