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