Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Instantiated Prefab Clones Not Rendering in Game view, But Only After Previous Instantiations.

Discussion in 'Scripting' started by CTV123, Mar 30, 2023.

  1. CTV123

    CTV123

    Joined:
    Oct 22, 2021
    Posts:
    7
    Hello, I am working on a small 2D game called "Protect the Square", whereas the name suggests the player must protect a square from circular enemy's via powerups, controlling the square itself, and controlling a paddle used to deflect the enemies. I am currently working on an endless mode, where the level is continuously generated from handmade prefabs as the player progresses though it, but I am running into an issue. After 4 to 5 segments(prefabs) are instantiated successfully, future segments, while still being instantiated, are invisible in the game view. As this feature is still a work in progress, I only have two prefabs made at the moment, and at the beginning of the level, both prefabs instantiate correctly, but after the 4 to 5 segment threshold is reached, the same prefabs which were previously instantiating correctly become invisible. Once this happens to one prefab it happens to all future prefabs, but collisions still work as expected. Despite being invisible in game view, the prefabs appear normal in the editor. I have tried saving my game and restarting Unity, but other than that, I'm clueless on what is causing this or how to troubleshoot and fix it. I have included my level generation script below, although I doubt that the script is the problem because even the problematic prefabs are instantiated in the correct positions. Any help or insight would be greatly appreciated.

    Thanks,
    CT

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelSpawner : MonoBehaviour
    6. {
    7.     public GameObject[] levelPrefabs; // Array of level prefabs
    8.     public Transform square; // Reference to the square object
    9.     public float triggerDistance; // Distance at which to trigger the next level piece
    10.     public Transform startingSpawnPoint; // Spawn point of the starting piece
    11.  
    12.     private Transform lastSpawnPoint; // Last spawn point
    13.  
    14.     void Start()
    15.     {
    16.         lastSpawnPoint = startingSpawnPoint;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         // Check if the square is close enough to the last spawn point to trigger the next level piece
    22.         if (Vector2.Distance(square.position, lastSpawnPoint.position) <= triggerDistance)
    23.         {
    24.             // Select a random level prefab
    25.             int index = Random.Range(0, levelPrefabs.Length);
    26.             GameObject levelPrefab = levelPrefabs[index];
    27.  
    28.             // Determine the position for instantiating the new level piece
    29.             Vector3 spawnPosition = lastSpawnPoint.position;
    30.  
    31.             // Instantiate the level prefab at the spawn position
    32.             GameObject newLevelPiece = Instantiate(levelPrefab, spawnPosition, Quaternion.identity);
    33.  
    34.             // Update the last spawn point
    35.             lastSpawnPoint = newLevelPiece.transform.Find("SpawnPoint");
    36.         }
    37.     }
    38. }
     
  2. CTV123

    CTV123

    Joined:
    Oct 22, 2021
    Posts:
    7
    To anyone who is having the same issue as I was: My problem was that the scale of my game was too large. You can start to have issues if your objects are greater than 10,000 units away from the origin, so you need to try to stay within that range. I myself wasn't having an issue until I got to about 30,000 units away from the origin, and 4 to 5 of my level segments just happened to be the number it took to get out that far. So, in short, if you're having a similar issue check your transform position and make sure it's not above 10K units on any axis!
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Thanks a lot for taking the time to come back and post-mortem it.

    Generally with single-precision floating point variables, you don't want to go more than a few thousand at most from (0,0,0). This is simply because the further you go, the less precision you have, by virtue of the dynamic range abilities of the float datatype. It can go really big and really tiny, but it can really only have about 6 to 9 decimal places of true accuracy, and you probably only want to rely on 4 or 5 decimal places.

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums

    Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    Literal float / double issues:

    https://forum.unity.com/threads/err...annot-be-implicitly-con.1093000/#post-7038139

    And thanks to halley for this handy little visual floating point converter:

    https://www.h-schmidt.net/FloatConverter/IEEE754.html
     
    CTV123 likes this.