Search Unity

Question Spawn areas that share the same code, intervene with each other in some way

Discussion in '2D' started by Warrocs1, Mar 30, 2023.

  1. Warrocs1

    Warrocs1

    Joined:
    Feb 24, 2019
    Posts:
    4
    Hi,
    So i am trying to create som generic code for boxes i can place which will spawn NPCS of different difficulties. So there are difficulties such as easy or hard, which currently governs their size, their localscale.
    However they tend to overwrite each other, so the box i have on very easy, spawns very hard instead. I suspect there is some problem with concurrency, as they do it on the same time it seems like. But i havent been able to locate the culprit. All the values are correct until they actually get spawned in the editor window.
    Here are some code snippets:
    upload_2023-3-30_9-1-12.png
    upload_2023-3-30_9-1-39.png
     
  2. Warrocs1

    Warrocs1

    Joined:
    Feb 24, 2019
    Posts:
    4
    Here is how it looks from the editor
    upload_2023-3-30_9-4-55.png
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Photographs of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    There is no concurrency in Unity, at least from the scripting standpoint. It is all linear code flow. Here's how it flows:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    This sounds like you have a bug! 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 <-- my money is here
    - 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.

    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 or 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.

    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)

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

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Why are you using a Static BoxCollider2D (that will produce collisions etc) just to define a rectangular area? That simply looks like an authoring thing.

    Just use a Rect in your script and simply draw it in OnDrawGizmos.
     
  5. Warrocs1

    Warrocs1

    Joined:
    Feb 24, 2019
    Posts:
    4
    Thanks for the tip. However i am using the BoxCollider2D because i want to be able to place it inside the editor. If it is purely in code like the Rect struct, is it still possible to place it and manipulate it inside the editor?
     
  6. Warrocs1

    Warrocs1

    Joined:
    Feb 24, 2019
    Posts:
    4
    Thanks for the suggestion, i will use code tags in the future :)

    Maybe i should have said that i already have debugged the code myself. I posted on this forum, because all the data values seemed to be right on the different lines of code while debugging, (I also used Debug.log()) and i came to the conclusion that it might be something else that is wrong. But i will try to debug again and see if i can find some more details about the problem :)
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Of course it is yes. That is what I suggested above. You're using "Bounds" which is an AABB. You can add a Bounds (or Rect) public field to your script and just like any other public field you've seen, you can edit it. You can draw it with the gizmo function I showed you.

    Here's a trivial example of a field using Bounds:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestThing : MonoBehaviour
    4. {
    5.     public Bounds SpawnBox;
    6.  
    7.     private void OnDrawGizmosSelected()
    8.     {
    9.         Gizmos.color = Color.yellow;
    10.         Gizmos.DrawWireCube(SpawnBox.center, SpawnBox.size);
    11.     }
    12. }
    13.  
    ... or transform the bounds by the transform:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestThing : MonoBehaviour
    4. {
    5.     public Bounds SpawnBox;
    6.  
    7.     private void OnDrawGizmosSelected()
    8.     {
    9.         Gizmos.color = Color.yellow;
    10.         Gizmos.DrawWireCube(transform.TransformPoint(SpawnBox.center), transform.TransformVector(SpawnBox.size));
    11.     }
    12. }
    13.  
    It's also super easy to just create a spawn-box type that you can use in any of your scripts which automatically gets draw/edited.