Agent destination OnTriggerEnter change variable problem

using UnityEngine;
using System.Collections;

public class VisitorAI : MonoBehaviour {

    NavMeshAgent agent;
    public GameObject sleepRestorePrefab;
    public float rest = 100f;


   

   


   
    void Start () {

        agent = GetComponent<NavMeshAgent> ();
   
    }

    void Update () {

        if (rest > 1f )
        {

            rest -= Time.deltaTime * 5;
            agent.SetDestination (Random.insideUnitSphere * 350);                   // Agent is currently wondering around as it is healthy
            gameObject.GetComponent<Renderer>().material.color = Color.green;       // Colour indicates that agent is completely healthy
        }

        if (rest < 5f )
            {


            gameObject.GetComponent<Renderer>().material.color = Color.black;       // Changes colour of agent once they get tired
   

            agent.SetDestination = sleepRestorePrefab.transform.position;           // Tells the agent to go to instantiated building's position


            {

                // OnTriggerEnter information - AI Enters trigger radius to get sleep
           

                void OnTriggerEnter(Collider other)
                   
                    if(other.tag == "sleepRecoveryRadius")                          // Detects whether agent is close enough to the building to increase sleep
                       
                    {
                        rest += Time.deltaTime * 10;                               // Increases sleep
                        Debug.Log ("Sleep is being restored");
                       
                    }



           

            }

            else if ( rest > 99f )

            {
                gameObject.GetComponent<Renderer>().material.color = Color.green;   // Agent is healthy again, so it goes back to original colour
                agent.SetDestination (Random.insideUnitSphere * 350);               // Tells the agent to wonder around again now it is healthy
            }

        }



   
    }
}

My brains a bit frazzled so I’m sucking it up and asking for help as I need a fresh pair of eyes to look at where I’ve gone wrong. I’m nearly finished with this little bit of code and to be helpful as it’s messy right now I’ve added some comments so everybody knows what’s going on.

Right now I’m entirely focused on getting this trigger I have set up to work, it’s a very simple idea, when the variable rest in question goes down, then the agent with the script attached will immediately head off to whichever object is labelled to restore the rest variable. Only problem is I just haven’t got the syntax right, I realised when I was messing around with this stuff that I needed a trigger so that the agents wouldn’t just automatically recover the variable right away but Unity just doesn’t like the way I’ve put it in here.

OnTriggerEnter is a function, it doesn’t go inside Update.

I get what you’re saying, but unfortunately I have no idea how to properly seperate the two functions so I can still have the agent detect whether it has entered the trigger or not, at least, in this context.

class blah blah...
{
void Update()
{...}

void OnTriggerEnter(...)
{...}

}

OnTrigger### functions are called separately in the game loop, you just need to tell the unityengine what it needs to do, it’ll figure out when to apply the code

Thank you! Looks like it was just another classic case of code positioning and having brackets missing. The errors are gone but now for some reason the agent decides to simply pause where it is while the rest variable ticks down. I’ll have a poke around and see if I can fix it myself first though.

Edit: After doing some digging it looks like my other problem is something to do with agent.Resume, I’ll let you guys know what I find if it suddenly fixes everything.

Edit 2: Nope, going to have to look up multiple waypoints in Unity I think.

I was over thinking it again -_- I had the agent.setDestination inside another if statement so they were coming into conflict with each other and confusing the agent.

Thanks for the help with the collider though, I got a chunk of code out of the way today.

Still running into the same pausing problems again >_< here is new code, I put debugging information to help solve it but it looks like only the first if statement is activating and only the material changes, the if statement seems to be ignoring SetDestination.

using UnityEngine;
using System.Collections;

public class VisitorAI : MonoBehaviour {

    NavMeshAgent agent;
    public Transform sleepRestorePrefabPosition;
    public float rest = 100f;



    void Start () {

        agent = GetComponent<NavMeshAgent> ();



    }

    void Update () {

        if (rest > 30f )
        {
            Debug.Log ( "I am healthy!" );
            rest -= Time.deltaTime * 5;
            agent.SetDestination (Random.insideUnitSphere * 350);                   // Agent is currently wondering around as it is healthy
            gameObject.GetComponent<Renderer>().material.color = Color.green;       // Colour indicates that agent is completely healthy
        }


        if (rest < 30f )
        {
            Debug.Log ("I am getting sleep now");
            agent.SetDestination ( sleepRestorePrefabPosition.position );           // Tells the agent to go to instantiated building's position
            gameObject.GetComponent<Renderer>().material.color = Color.black;       // Changes colour of agent once they get tired
      
            if ( rest > 90f )
          
            {
                gameObject.GetComponent<Renderer>().material.color = Color.green;   // Agent is healthy again, so it goes back to original colour
                agent.SetDestination (Random.insideUnitSphere * 350);               // Tells the agent to wonder around again now it is healthy
            }
      
        }

      
    }









    // Building trigger information


    void OnTriggerEnter(Collider other)

    {

        if (other.tag == "sleepRecoveryRadius") {                          // Detects whether agent is close enough to the building to increase sleep
            rest += Time.deltaTime * 20;                                       // Increases rest
            Debug.Log ("Sleep is being restored");
  
        }

    }




}
if (rest < 30f)
{
    // rest is not changed
    if (rest > 90f)
    {
        // This code will never be executed because rest can not be smaller than 30 and greater than 90
    }
}
1 Like