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. Dismiss Notice

Question Spawning enemies out of sight on top of navmesh

Discussion in 'Scripting' started by rodrigoaben, Sep 1, 2023.

  1. rodrigoaben

    rodrigoaben

    Joined:
    Sep 25, 2018
    Posts:
    2
    I'm trying to instantiate enemies out of camera sight as long as they are on top of a navmesh. This is current code, but it does nothing. What am I getting wrong?

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class EnemySpawner : MonoBehaviour {
    9.     private int waveNumber = 0;
    10.     public int enemiesAmount = 0;
    11.     public GameObject Enemy;
    12.     public Camera cam;
    13.     // Use this for initialization
    14.     void Start () {
    15.         cam = Camera.main;
    16.         enemiesAmount = 0;
    17.     }
    18.  
    19. // Update is called once per frame
    20. void Update () {
    21.         float height = cam.orthographicSize + 1;
    22.         float width = cam.orthographicSize * cam.aspect + 1;
    23.         if (enemiesAmount==0) {
    24.             waveNumber++;
    25.             for (int i = 0; i < waveNumber; i++) {  
    26.  
    27.                 NavMeshHit hit;
    28.  
    29.                 if(NavMesh.Raycast(cam.transform.position, new Vector3(cam.transform.position.x + Random.Range(-width, width),3,cam.transform.position.z+height+Random.Range(10,30)), out hit, NavMesh.AllAreas)){
    30.  
    31.                     Debug.Log("Hit");
    32.                 Instantiate(Enemy, new Vector3(cam.transform.position.x + Random.Range(-width, width),3,cam.transform.position.z+height+Random.Range(10,30)),Quaternion.identity);
    33.                 enemiesAmount++;
    34.                 }
    35.             }
    36.             }
    37.         }
    38.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Sounds like you wrote a bug! Here's how you can get to the bottom of it:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    No offense, but would love to see this in its own thread, and then just linked to when needed. The scrolling in almost every thread is a bit painful, especially on mobile.

    With a link there is a chance people won't follow the link and never read the info, but with such an amount of text, I doubt many will read it anyway, despite the useful information.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    If your code is doing nothing and there are no error messages, then it's probably either:
    1. one or more of your "if" conditions are not evaluating to true.
    2. Your for loop is iterating 0 times.
    3. Your code is not running at all.
    To find out if any of these things are happening you could use Debug.Log to track the flow of execution. You can put debug messages in various parts of your code and if you see the messages in the console then you'll know whether that part of the code is executing or not.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    No offense but I'd like to see people learn how to debug.

    It is not an optional part of software engineering and yet it frequently surprises folks to realize they made errors and that only they can find those errors.

    When an OP writes boilerplate stuff like:

    ... then they're gonna get boilerplate back to go find more information.

    If on the other hand OP came in with something like,

    "I am raycasting to the navmesh from position A and yet my enemy spawns at B."

    Then we can talk and drill into that process and I might note, "Hey, you're adding a random offset to a position!"

    But if you post horribly-complex code like this:

    I can only barely see that
    +Random.Range(10,30)
    term, which probably has a LOT to do with their problem.... ESPECIALLY because it looks like pure copy/pasta from the line above it.

    But seriously, you wanna untangle that here in the forum? Code written like that would be an immediate rejection of any pull request on my team.
     
  6. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    I am aware of the content of your message, but can it be a link to that content? would be great.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If people actually followed links then they wouldn't be in here posting...

    ... Right?
     
  8. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    On the other hand, if people actually read what you wrote, they wouldn't be posting here either, coz they already come across the content on every other post or so. Just my 2cts.
    When I first joined the platform I thought there was some kind of "who posts the most words" contest, coming across your default post so often. It is okay though, you do you.