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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved SpawningProblem

Discussion in 'Scripting' started by jeti20, Oct 9, 2022.

  1. jeti20

    jeti20

    Joined:
    Aug 21, 2021
    Posts:
    12
    Hi,
    I am creating an endlessruner. But I have a problem and I don't know how to fix it anymore. I want the "Nitro" object to spawn along with the road, but in random places. As you can see in the video, it spawns only in the range of the Z axis (0.200) on the width of 579, 562. I do not understand why it does not appear in the next 200 meters on the Z axis together with the road. despite zPos + = 200;
    script.jpg


    OBJECT SPAWNING
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NitroSpawner : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Nitro;
    9.     public int zPos = 200;
    10.     public bool creatingSection = false;
    11.     //random X i dodawanie Z
    12.     // Start is called before the first frame update
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (creatingSection == false)
    18.         {
    19.             creatingSection = true;
    20.             StartCoroutine(GenerateNitro()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
    21.         }
    22.     }
    23.     IEnumerator GenerateNitro()
    24.     {
    25.  
    26.         Instantiate(Nitro, new Vector3(Random.Range(579, 562), 305.5273f, Random.Range(0, 200)), Quaternion.Euler(0, 0, 0));
    27.         zPos += 200;
    28.         yield return new WaitForSeconds(1);
    29.         creatingSection = false;
    30.     }
    31. }
    32.  
    ROAD SPAWNING
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GenerateLevel : MonoBehaviour
    6. {
    7.     public GameObject road;
    8.  
    9.     public int zPos = 200;
    10.     public bool creatingSection = false;
    11.  
    12.     void Update()
    13.     {
    14.         if (creatingSection == false)
    15.         {
    16.             creatingSection = true;
    17.             StartCoroutine(GenerateSection()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
    18.         }
    19.     }
    20.  
    21.     IEnumerator GenerateSection()
    22.     {
    23.        
    24.         Instantiate(road, new Vector3(0, 0, zPos), Quaternion.Euler(0, 0, 0));
    25.         zPos +=200;
    26.         yield return new WaitForSeconds(1);
    27.         creatingSection = false;
    28.  
    29.        
    30.  
    31.     }
    32. }
    33.  
    script.jpg
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    Hmm, is there code missing from those scripts? I see zPos being assigned but not being used anywhere after the assignment. Like, you are assigning += 200, after the instantiated fact. Maybe? It would seem to me that those additions to the value should be before the object is instantiated, OR, the objects position should be updated with the new values after it is instantiated.
     
    jeti20 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Welcome to debugging!

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

    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 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/

    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.
     
    jeti20 likes this.
  4. jeti20

    jeti20

    Joined:
    Aug 21, 2021
    Posts:
    12
    Thank you guys. I solved this problem. Corrected code for spawning object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NitroSpawner : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Nitro;
    9.     public int zPos = 0;
    10.     public bool creatingSection = false;
    11.     public float Wide1 = 579;
    12.     public float Wide2 = 562;
    13.     //random X i dodawanie Z
    14.     // Start is called before the first frame update
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (creatingSection == false)
    20.         {
    21.             creatingSection = true;
    22.             StartCoroutine(GenerateNitro()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
    23.         }
    24.     }
    25.     IEnumerator GenerateNitro()
    26.     {
    27.  
    28.         Instantiate(Nitro, new Vector3(Random.Range(Wide1, Wide2), 307.5273f, Random.Range(0, zPos)), Quaternion.Euler(180, 0, 0));
    29.        
    30.         yield return new WaitForSeconds(1);
    31.         zPos += 200;
    32.         creatingSection = false;
    33.     }
    34. }
    35.  
    But now...
    I wonder why the prefab position shows 0,0,0 although it is not in 0,0,0,. If I select an element of this prefab it shows the correct parameters of the position, if I mark the prefab it shows 0,0,0 although it is not in one. What do I need to do for my prefab to actually be at 0,0,0 and show 0,0,0
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    The children of the prefab show their local position, in relation to their parent. The top most parent will show its world position. No?
     
    Kurt-Dekker likes this.
  6. jeti20

    jeti20

    Joined:
    Aug 21, 2021
    Posts:
    12
    Hi, I solved this problem by creating new object in position 0,0,0, then make it prefab. I turned out it does metter where you crated your prefab :v Big Thanks!
     
    Kurt-Dekker likes this.