SpawningProblem

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;

OBJECT SPAWNING

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NitroSpawner : MonoBehaviour
{

    public GameObject Nitro;
    public int zPos = 200;
    public bool creatingSection = false;
    //random X i dodawanie Z
    // Start is called before the first frame update 

    // Update is called once per frame
    void Update()
    {
        if (creatingSection == false)
        {
            creatingSection = true;
            StartCoroutine(GenerateNitro()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
        }
    }
    IEnumerator GenerateNitro()
    {

        Instantiate(Nitro, new Vector3(Random.Range(579, 562), 305.5273f, Random.Range(0, 200)), Quaternion.Euler(0, 0, 0));
        zPos += 200;
        yield return new WaitForSeconds(1);
        creatingSection = false;
    }
}

ROAD SPAWNING

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateLevel : MonoBehaviour
{
    public GameObject road;

    public int zPos = 200;
    public bool creatingSection = false;
  
    void Update()
    {
        if (creatingSection == false)
        {
            creatingSection = true;
            StartCoroutine(GenerateSection()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
        }
    }

    IEnumerator GenerateSection()
    {
       
        Instantiate(road, new Vector3(0, 0, zPos), Quaternion.Euler(0, 0, 0));
        zPos +=200;
        yield return new WaitForSeconds(1);
        creatingSection = false;

       

    }
}

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.

1 Like

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://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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://discussions.unity.com/t/839300/3

When in doubt, print it out!™

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

1 Like

Thank you guys. I solved this problem. Corrected code for spawning object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NitroSpawner : MonoBehaviour
{

    public GameObject Nitro;
    public int zPos = 0;
    public bool creatingSection = false;
    public float Wide1 = 579;
    public float Wide2 = 562;
    //random X i dodawanie Z
    // Start is called before the first frame update 

    // Update is called once per frame
    void Update()
    {
        if (creatingSection == false)
        {
            creatingSection = true;
            StartCoroutine(GenerateNitro()); // w corutine musi byc opóźneinie nie mozna tego zrobić w metodzie, opóznienie pojawiania sie mapy
        }
    }
    IEnumerator GenerateNitro()
    {

        Instantiate(Nitro, new Vector3(Random.Range(Wide1, Wide2), 307.5273f, Random.Range(0, zPos)), Quaternion.Euler(180, 0, 0));
       
        yield return new WaitForSeconds(1);
        zPos += 200;
        creatingSection = false;
    }
}

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

The children of the prefab show their local position, in relation to their parent. The top most parent will show its world position. No?

1 Like

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!

1 Like