Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

changing navmesh speed on instantiated prefabs

Discussion in 'Scripting' started by ChuckieGreen, Mar 27, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    Hi, I am hoping someone can help me out with changing the nav mesh speed on instantiated objects.

    I have an enemy who is using the nav mesh to navigate to the player. This script (AI script) is attached to the enemy
    I have a spawner which spawns the enemies, The spawning script is attached to the spawner.


    Spawner script
    Code (CSharp):
    1. RandomRangeAmountZ = Random.Range(-77, 5);
    2.         RandomRangeAmountX = Random.Range(-23, 52);
    3.         if (stopenemyspawner.Day < 3)
    4.         for (int i = 0; i < 10; i++)
    5.         {
    6.            RandomRangeAmountZ = Random.Range(-77, 5);
    7.            RandomRangeAmountX = Random.Range(-23, 52);
    8.            Instantiate(prefab, new Vector3(RandomRangeAmountX, 0, RandomRangeAmountZ), Quaternion.identity);
    9.            VampCounter++;        
    10.         }
    I would like to have each instantiated prefab from the loop have a different speed from the nav mesh. I thought just setting the speed to a random interger using the code below in the AI script in the update function, would assign each instantiated prefab a random speed. But they are all assigned the same speed. So basically I know how to code what I want, I just have no idea how i would set it up to work. If anyone knows how I can assign the instantiated prefabs a random NavMesh speed, it would be greatly appreciated. :)

    Code (CSharp):
    1. RandomNumberForVampSpeed = Random.Range(1, 10);
    2.         nav.speed = RandomNumberForVampSpeed;
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Are you getting the NavMeshAgent component?
     
  3. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    Im getting that on the AI script

    Code (CSharp):
    1. NavMeshAgent nav;
    2.  
    3. private void Awake()
    4.     {      
    5.         nav = GetComponent<NavMeshAgent>();      
    6.                
    7.     }
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Your code should work. Where are you calling it. I don't see it in the Instantiate part, but you need to get the navmesh agent of each instantiated prefab.
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay, lets totally ignore the spawner for now, as the spawner is just instantiating the prefabs. Once they're made, from what I gather, they're basically on their own to handle themselves through the AI script. So it's more important to us to see the AI script. As something simple like

    Code (CSharp):
    1. private NavMeshAgent nav;
    2.  
    3.         private void Awake()
    4.         {
    5.             if(!(nav = GetComponent<NavMeshAgent>()))
    6.             {
    7.                 Debug.LogWarningFormat("Found no {0} attached to {1}", typeof(NavMeshAgent).Name, gameObject.name);
    8.             }
    9.             else
    10.             {
    11.                 nav.speed = Random.Range(1, 10);
    12.             }
    13.         }
    would work.

    https://answers.unity.com/questions/1274249/how-do-i-change-nav-mesh-agent-speed-with-c-script.html
     
  6. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    Putting this in the update function worked, I was putting it in the update function, so guess that was my issue :rolleyes:

    Thanks for the help :)