Variable updated in method not recognized in Update

Below is script A. When it collides with a "frutgtable" or "shugcarb", it first goes to the TurnBoxOff method inside of script A  and changes the value of collidersOff to 1. In turn, this triggers the if statement in the Update method of script A and turns the box collider off. The issue I am running into is when script A calls leftScript.TurnBoxOf(); . 

This takes it to the TurnBoxOff method in script B and does successfully change the value of leftCollidersOff within the method and I can tell this because the first debug in that method sends a value of 0 and the second sends a value of 1. But the debug in the update of script does not recognize this. It keeps sending a value of 0 and the if statement if(leftCollidersOff == 1) is never activated. 

I can not figure out why it works perfectly in script A and fails in script B since essentially the exact same thing is happening is each script. The only difference is that script B is being called. Any ideas? Thanks for your help! 


Script A

{

public int collidersOff = 0, rightEyeSeesFood;
public FirstBabyScript firstBabyScript;
public VisionLeft leftScript ;

private void Start()
{
firstBabyScript = firstBabyScript.GetComponent<FirstBabyScript>();
leftScript = leftScript.GetComponent<VisionLeft>();
}

private void Update()
{

if (collidersOff == 1){
GetComponent<BoxCollider>().enabled = false;
}

else if (collidersOff == 0){
GetComponent<BoxCollider>().enabled = true;
}
}


private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Frutgtable") || col.CompareTag("ShugCarb") )   {
TurnBoxOff();
leftScript.TurnBoxOff();
firstBabyScript.FindFood();
Debug.Log("Hit Food right");
}
} 

public void TurnBoxOff()
{
collidersOff = 1;
Debug.Log("Turned right box off");
}

public void TurnBoxOn()
{
collidersOff = 0;
Debug.Log("Turned right box on");
}

}


Script B



{

public FirstBabyScript firstBabyScript;
public int leftEyeSeesFood, leftCollidersOff = 0;
public VisionRight rightScript;

private void Start()
{
firstBabyScript = firstBabyScript.GetComponent<FirstBabyScript>();
rightScript = rightScript.GetComponent<VisionRight>();
}

private void Update()
{
Debug.Log(leftCollidersOff);
if (leftCollidersOff == 1){
GetComponent<BoxCollider>().enabled = false;
}

else if (leftCollidersOff == 0){
GetComponent<BoxCollider>().enabled = true;
}
}

private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Frutgtable") || col.CompareTag("ShugCarb")){
TurnBoxOff();
rightScript.TurnBoxOff();
firstBabyScript.FindFood();
Debug.Log("Hit Food right");

}
}

public void TurnBoxOff()
{
Debug.Log(leftCollidersOff);
leftCollidersOff = 1;
Debug.Log(leftCollidersOff);
Debug.Log("Turned left box off");
}

public void TurnBoxOn()
{
leftCollidersOff = 0;
Debug.Log("Turned left box on");
}

}
  • Jonathan Gonzalez(jgonzalez) replied

    I'm a bit confused on how these scripts work together. To simplify  it, you're turning off the box collider of the object containing this script once something with the tag of "frutgtable" or "shugcarb"  correct? The scripts you posted seem to do that just fine after testing it, although it can be simplified like this:

        void OnTriggerEnter (Collider col)
        {
            if (col.CompareTag ("Frutgtable") || col.CompareTag ("ShugCarb")) {
                TurnBoxOff ();
                GetComponent<BoxCollider> ().enabled = false;
                Debug.Log ("Hit Food right");
            }
        }
    


    I left "TurnBoxOff()"in the OnTriggerEnter, but if it's only being used to enable/disable the collider it can be removed. Since I don't have any of the other scripts referenced I can't test those. Since both the scripts posted are very similar, you could use one generic script for both objects instead of separate scripts. No need to use Update or even separate methods if all you're doing is enabling and disabling the collider. OnTriggerEnter can do everything you need. 

  • Hunter & Tiff Eidmann(eithman) replied

    After taking a break over the weekend and coming back I realized you are right and that the script has multiple unnecessary parts. I changed the script on the right collision box to the following. 

    
    
    
    public class VisionRight : MonoBehaviour
    {
    
    public int collidersOff = 0, rightEyeSeesFood;
    public FirstBabyScript firstBabyScript;
    public VisionLeft leftScript ;
    public BoxCollider leftCollider;
    
    private void Start()
    {
    firstBabyScript = firstBabyScript.GetComponent<FirstBabyScript>();
    leftScript = leftScript.GetComponent<VisionLeft>();
    leftCollider = leftCollider.GetComponent<BoxCollider>();
    }
    
    private void OnTriggerEnter(Collider col)
    {
    if (col.CompareTag("Frutgtable") || col.CompareTag("ShugCarb") )   {
    TurnBoxOff();
    Debug.Log("returned from TurnBoxOff");
    firstBabyScript.StartFindFood();
    Debug.Log("returned from find food");
    }
    } 
    
    public void TurnBoxOff()
    {
    GetComponent<BoxCollider>().enabled = false;
    leftCollider.enabled = false;
    Debug.Log("Turned right box off");
    }
    
    public void TurnBoxOn()
    {
    GetComponent<BoxCollider>().enabled = true;
    leftCollider.enabled = true;
    Debug.Log("Turned right box on");
    }
    
    }


    This successfully deactivates the collision boxes once OnTriggerEnter is entered. Now the problem I am having is that StartFindFood is called and within the StartFindFood method the FindFood coroutin is called but none of the code in that coroutine is executed. Not even the Debug.Log at the very top of the FindFood coroutine. Below is the relevant code from firstBabyScript which contains StartFindFood and FindFood.

    
    
    public void StartFindFood()
    {
    Debug.Log("In start find food");
    StartCoroutine("FindFood");
    }
    public IEnumerator FindFood()
    {
    Debug.Log("In FindFood");
    
    if (elapsedTime > 0){
    Debug.Log("FindFoodSkipped!");
    yield break;
    }
    speed = 0;
    rayLength = 15;
    while (elapsedTime < 1){
    elapsedTime += Time.deltaTime;
    transform.position += transform.right * Time.deltaTime;
    yield return null;
    }
    elapsedTime = 0;
    
    hit = 90;
    
    if (leftEye == 1){
    hit = hit * -1;
    }
    
    leftEye = 0;
    
    rotateAmount = new Vector3(0, transformObj.eulerAngles.y + hit, 0);
    startRot = transform.rotation;
    endRot = Quaternion.Euler(rotateAmount);
    
    
    while (elapsedTime < 5){
    elapsedTime += Time.deltaTime;
    Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * rayLength, Color.red);
    if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), rayLength, foodRayMask)){
    didntFindFood = 0;
    break;
    }
    transform.localRotation = Quaternion.Lerp(startRot, endRot, elapsedTime / 5);
    yield return null;
    }
    
    elapsedTime = 0;
    speed = 20;
    if (didntFindFood == 1){
    collisionBoxTimer = 300;
    }
    
    didntFindFood = 1;
    
    }


    I do get the Debug.Log messages saying "in start find food" and "returned from find food". Do you have any idea why it is that none of the code in FindFood is being executed? Thanks!



  • Hunter & Tiff Eidmann(eithman) replied

    I just figured out the issue. When I was manually assigning scripts in the inspector panel I was doing it from the wrong location. So for leftScript I made a brand new empty, applied the script to it and then dropped that into the public script variable rather than dropping the actual box collider with the script that is on my prefabbed animal. 

  • Jonathan Gonzalez(jgonzalez) replied

    eithman Great. I would look at your console (Window > Console) as it will usually through an error or warning when you're having these kinds of issues. You might've seen a console error showing up in red at the bottom left of the Unity editor.