- Home /
Can read but cannot pass variables between scripts...
Hey. So I have a public bool assigned to the public bool from a script connected with another gameObject. I want to change the boolean value of the remote script when my raycast encounters an "enemy" tag. After testing I've determined that I am getting the true value of BattleTime.On() and that my raycasting is working as intended, but I cannot alter the value of BattleTime.On(). Here is my player script:
public bool battletime;
public void Update() {
battletime = GameObject.Find("Manager").GetComponent<BattleTime>().on;
RaycastHit hitInfo1;
Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
if (Physics.Raycast (ray, out hitInfo1, Mathf.Infinity)) {
if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
//Physics2D.Raycast (ray1, out hitInfo1, Mathf.Infinity)) {
battletime = true;
Debug.Log (battletime);
}} else {
battletime = false;
Debug.Log (battletime);
}
And here is the constant script, located in the Manager gameObject:
public bool on;
void Update(){
if (on == true)
Debug.Log ("it is true");
else
Debug.Log ("lies");
}
Turning 'on' into a static script does not help. Anyone got any ideas?
Answer by getyour411 · Feb 01, 2014 at 09:04 AM
This is how I do it. Instead of this
battletime = GameObject.Find("Manager").GetComponent<BattleTime>().on;
I do
battletime = GameObject.Find("Manager").GetComponent<BattleTime>();
and then instead of
battletime
I do
battletime.on == ....
I haven't tried the method you are using with the direct reference, not saying it's wrong, just the method that I Try this site too: http://unitygems.com/script-interaction1/
Thanks for the info, but it still isn't working. Also, I'm trying to alter the value of BattleTime().on, not compare it to anything. Should I just make the battletime formulation local to move and then have other objects constantly reference it...?
Let me try explaining it again.
The move script looks for raycasthits on update. It can distinguish between an 'enemy' tag and anything else. That works The battletime bool successfully references GameObject.$$anonymous$$anager().on, I have verified this by having the move script produce output corresponding to BattleTime().on and altering on's value directly in the inspector. That works
BUT If I want a condition met in the move script to alter the value of BattleTime().on, it won't work. I have read the manual on script interaction, and the only thing I haven't tried is converting BattleTime().on to a static variable, which I -thought- was sufficient-but-not-necessary. God this is irritating.
I've got a move script, which includes:
public bool battletime;
public void Update() {
battletime = GameObject.Find("$$anonymous$$anager").GetComponent<BattleTime>().on;
RaycastHit hitInfo1;
Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
if (Physics.Raycast (ray, out hitInfo1, $$anonymous$$athf.Infinity)) {
if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
battletime = true;
Debug.Log (battletime);}}
else {
battletime = false;
Debug.Log (battletime);
}
And my $$anonymous$$anager object has a BattleTime script, which contains:
public bool on;
void Update(){
if (on == true)
Debug.Log ("it is true");
if (on == false)
Debug.Log ("lies");
}
@getyour411 answer is very close. Some variables are by value, so are by reference. Boolean variables are by value, so you are getting a copy of your variable. I believe you want:
public BattleTime battletime;
public void Update() {
battletime = GameObject.Find("$$anonymous$$anager").GetComponent<BattleTime>();
RaycastHit hitInfo1;
Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
if (Physics.Raycast (ray, out hitInfo1, $$anonymous$$athf.Infinity)) {
if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
//Physics2D.Raycast (ray1, out hitInfo1, $$anonymous$$athf.Infinity)) {
battletime.on = true;
Debug.Log (battletime);
}} else {
battletime.on = false;
Debug.Log (battletime);
}
Note how 'battletime' is of type 'BattleTime' not bool.
Thanks a lot, both of you. That worked. Gonna have to cogitate for a little while.
@Acquial - If your question is answered, click on the checkmark next to the left of the answer to close it out. Thanks.