- Home /
Variable from a external script not being effected
I have script A (ChildCollision) which is a collision script that I put on my child objects of my prefab. In that script I tell a variable bool isEmptySlot1 to be false when a collision happens. But in Script B (Formation_AI) where the variable is it does not change for some reason it remains true. I've put debug log on the collision script and THAT one says it turns false, but the variable in script B (Formation_AI) does not, I don't know what the problem is, basically I set the script B (Formation_AI) as a instance that is referenced in Script A (ChildCollision). Here is some of my Code.
ChildCollison.cs
public class ChildCollision : MonoBehaviour {
// Access Formation AI
public Formation_AI get;
void Awake()
{
Formation_AI get = GetComponent<Formation_AI>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.collider.tag == "Enemy")
{
get.isEmptySlot1 = false;
Debug.Log("Collided" + other);
Debug.Log("Emptyslot" + get.isEmptySlot1);
}
}
}
That is script A and the variable says false but here in the next script its not getting changed to false it stays true.
// This right here is in Script B (Formation_AI)
Formation_AI.cs
public bool isEmptySlot1 = true; // if the slot is empty it can be filled
My third script (Fighter_AI) where it is supposed to work uses that bool but its not working because its not changing Formation_AI isEmptySlot1 to false from script ChildCollisionScript
Fighter_AI.cs
void Update () {
transform.LookAt(playerTarget);
if (script.isEmptySlot1 == true)
{
transform.position = Vector3.Lerp(transform.position, FormTarg1.position, Time.deltaTime * smoothTranslateSpeed);
Debug.Log("FighterSlot" + script.isEmptySlot1);
}
else if (script.isEmptySlot1 == true)
{
transform.position = Vector3.Lerp(transform.position, FormTarg2.position, Time.deltaTime * smoothTranslateSpeed);
}
else if (script.isEmptySlot1 == true)
{
transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);
}
if (playerDist)
{
float dist = Vector3.Distance(playerDist.position, transform.position);
//Debug.Log("Distance to other: " + dist);
}
}
The Debug.Log here says true when I clearly change it to false on collision, it stays true when its collision happens
So the MAIN QUESTION is how do you change a variable that belongs to another script from another script using GetComponent
All of these scripts are attached to the same GameObject?
Also your Update looks strange (you are testing the same variable in each if/else if). What is script and how did you get it?
Oh dear not sure my brain quite followed that!
What I presume is happening is that you are modifying an isEmptySlot1 variable on one script but then reading it on a different one. The way you have the FormationAI get = in the Awake is hiding the actual variable - so you aren't really getting it there.
what you should do is change the line:
Formation_AI get = GetComponent<Formation_AI>();
to
get = GetComponent<Formation_AI>();
BUT, this would only work if the script were on the same object.. since you say it is on the PARENT, you could ins$$anonymous$$d say:
get = transform.parent.gameObject.GetComponent<Formation_AI>();
that should work I think
As for if it were on a different object, you could find that object by name, or by tag, usually..
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html
once you have a reference to the GameObject (such as "hand", in the first link), you can then use that, as in:
hand.GetComponent<whatever>...
Answer by Seth-Bergman · Oct 01, 2012 at 06:04 AM
(from comments)
what you should do is change the line:
Formation_AI get = GetComponent<Formation_AI>();
to
get = GetComponent<Formation_AI>();
BUT, this would only work if the script were on the same object.. since you say it is on the PARENT, you could instead say:
get = transform.parent.gameObject.GetComponent<Formation_AI>();
that should work I think
As for if it were on a different object, you could find that object by name, or by tag, usually..
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html
once you have a reference to the GameObject (such as "hand", in the first link), you can then use that, as in:
hand.GetComponent<whatever>...
(Glad I could help, good luck!)
Your answer
Follow this Question
Related Questions
updating a gameobject variable from another script attached to another object 3 Answers
Is it possible to set variables for classes? 2 Answers
GetComponent from class 1 Answer
Issues accessing a variable in another script 0 Answers
Accessing variables in another script using GetComponent() 0 Answers