Accessing variable from another gameobject does not work
I`ve tryed different methods, but none of them worked for some reason. What am i doing wrong?
Script 1:
public class FloorProtector : MonoBehaviour {
GameObject temp = GameObject.Find("Cube");
void Start() {
BoxBehavior script = temp.GetComponent<BoxBehavior>();
script.isColliding = true;//testing if it works or not
}
...
Script 2:
public class BoxBehavior : MonoBehaviour
{
public bool isColliding = false;
...
It's not very helpful to just say "it doesn't work". Tell us what does happen.
If the line script.isColliding = true;
(in FloorProtector.Start()) runs without throwing a NullReferenceException then it does set that value.
But maybe not on the component that you intend it to.
Or, if it is the right component, maybe something else is changing it back before you inspect it.
Answer by Lewwwer · Sep 07, 2016 at 06:59 PM
I think the problem is that you tried to find the Cube in the variables. You shoud allways use the GameObject.Find() in the Start() and in the Update()
so the code is:
public class FloorProtector : MonoBehaviour {
GameObject temp;
void Start() {
temp = GameObject.Find("Cube");
BoxBehavior script = temp.GetComponent<BoxBehavior>();
script.isColliding = true;//testing if it works or not
}
Thanks for your reply, but it didn't help for some reason, maybe my unity editor is acting weird again
Tell us what happened when you tried it.
Lewwer's code should work IF
1) you have a FloorProtector component on an active GameObject and 2) you have an active GameObject called "Cube" which has a BoxBehaviour component at the time when the FloorProtector's Start() function is called.
If those conditions are met and it's still not working, try adding some logging (first of all, to verify that that Start function is getting called).
Whatever's going on here, it's not to do with accessing another component's variables (your script.isColliding = true
is fine).
Here is the first script http://pastebin.com/Eu6rL9HS here is the second: http://pastebin.com/Yd6imW9J I am trying to change the variable "isColliding" in the second script so that either first or the second if statement is executed. But right now it doesn`t change, Debug.Log(isColliding) in the second script shows false when i shoot a box, which is colliding with the GameFloor (wich has the FloorProtector script attached to it). Sorry for poor english)
Answer by nenok1 · Sep 08, 2016 at 12:49 PM
Try to make refference in the Editor:
public class FloorProtector : MonoBehaviour {
[SerializeField]
BoxBehavior script;
void Start() {
script.isColliding = true;
}
}
If you really want to use Find, make shure that:
there is only 1 GameObject named "Cube" (because Find will return first match);
BoxBehavior is on the "Cube" (not its child or parent).
I have more then 1 cube in the scene. This code also doesn`t work(
Does it throw "Null refference" or "$$anonymous$$issing refference" exception? Anyway it seems like BoxBehavior (or another guy) is changing value back. Try to log all the changes and see their source:
public class BoxBehavior : $$anonymous$$onoBehaviour {
private bool _isColliding = false;
public bool isColliding
{
get { return _isColliding; }
set
{
Debug.Log("isColliding is being changed: from "
+ _isColliding
+ " to "
+ value);
_isColliding = value;
}
}
}
Here is how it works: https://yadi.sk/i/cXxbR3qiuu$$anonymous$$jn
Here are 2 screenshots from my unity editor https://yadi.sk/d/5Qj9A-CJuubkg (is the link accessible?) i tried putting this code into my BoxBehavior script like that http://pastebin.com/yNye5Xi6 but it didn`t change anything. I`m feeling bad for taking your time, thanks a lot for your support. I`m a noob right now thats why i`m having so much trouble with simple things
So what are wanting to achieve with the Find() call, given that you have more than 1 Cube object? And when you say that isColliding isn't changing, have you checked all your Cube objects?
I thought that Find() will grab the script from any object and it will be fine
Ive tried this code again, in BoxBehavior i
ve put Debug.Log(isColliding) in the Update function it doesn`t change, it is still false, i don`t change it in any other script. It is just "public bool isColliding = false;" in one script and
[SerializeField] BoxBehavior script;
void Start() {
script.isColliding = true; }
Ive even deleted everything from FloorProtector script except for this code, just to change the bool value but it just doesn
t work i have no clue why