- Home /
one var that is local in multiple objects still used by other scripts?
so i have multiple objects( Grid of cubes) that i want to have a variable called canUse; what i am asking is if i can have this one variable be local to the gameObject, but then acces each variable in another script?
I don't understand, you want it to be local, but you also want others to be able to see it? I'm sure what you want to do is possible, but I need you to explain it a bit better.
ps anybody who looks at this question it was a stupid question as my script just wasn't functioning as i thought it would. variables are assigned to individual gameObjects as the below answers have said sorry about that.
Answer by gfr · Oct 31, 2011 at 04:40 AM
If you're raycasting for another object, you can get the hit gameobject from the hit-info and from there the script component.
So assuming a script CubeScript
on your cubes:
var canUse = true; // change in inspector as needed
// ... more stuff
... your raycasting script can do:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
var go = hit.transform.gameObject;
if (go.tag == "Foo") { // some test for hitting cubes
var cubeScript = go.GetComponent(CubeScript);
if (cubeScript && cubeScript.canUse) {
// do stuff
}
}
}
but will the first script's variable be only on that one object and then i can dynamically change the true and false of individual objects?
That's always the case- you need to specifically mark a variable 'static' to make it work in any other way!
Try to edit it into your question?
Also, try to figure out via logging when & why exactly the values are changed for the other objects. It's a bit hard to guess at what's going wrong with only seeing one part of the whole thing.
Your answer

Follow this Question
Related Questions
Lego-building System. 1 Answer
How to set a Transform variable with code (C#) 3 Answers
Grid matching issue 0 Answers
What is wrong with my code? 1 Answer
Adding Imperfections? 1 Answer