Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug GameObject not rendering

Discussion in 'Universal Render Pipeline' started by Russian_Echo21, Mar 20, 2023.

  1. Russian_Echo21

    Russian_Echo21

    Joined:
    Mar 20, 2023
    Posts:
    1
    I've been working on a small game project to learn how to use Unity and how to use C# for game dev. I am trying to get my enemy sprites to instantiate, and they are, but they don't show in the game view for some reason. I checked the camera Z position and its correct, I've made sure that the sprite render is set to be on, I am instantiating the sprites inside of the camera view. All of the trouble shooting things that I have been told I have tried and it is still not working.
    The code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EnemyLogic : MonoBehaviour
    7. {
    8.     public GameObject enemyPrefab;
    9.     public float respawnTime;
    10.     public UILogic logic;
    11.     private GameObject lastActualEnemyCreated;
    12.  
    13.     float screenWidth;
    14.     float screenHeight;
    15.     private float timer = 0;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         respawnTime = Random.Range(1f, 5f);
    20.         screenHeight = Screen.height;
    21.         screenWidth = Screen.width;
    22.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<UILogic>();
    23.         spawn();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void FixedUpdate()
    28.     {
    29.         if (logic.checkIfAlive() && !logic.Paused)
    30.         {
    31.             if (timer < respawnTime)
    32.             {
    33.                 timer += Time.deltaTime;
    34.             }
    35.             else
    36.             {
    37.                 spawn();
    38.                 timer = 0;
    39.             }
    40.         }
    41.     }
    42.  
    43.     private void spawn()
    44.     {
    45.         int side = Random.Range(0, 4);
    46.         float spawnX = 0f;
    47.         float spawnY = 0f;
    48.  
    49.         switch (side)
    50.         {
    51.             case 0: //Top side
    52.                 spawnX = Random.Range(0f, screenWidth-1f);
    53.                 spawnY = screenHeight;
    54.                 break;
    55.             case 1: //right side
    56.                 spawnX = screenWidth;
    57.                 spawnY = Random.Range(0f, screenHeight-1f);
    58.                 break;
    59.             case 2: //Bottom side
    60.                 spawnX = Random.Range(0f, screenWidth-1f);
    61.                 spawnY = 0f;
    62.                 break;
    63.             case 3: //Left side
    64.                 spawnX = 0f;
    65.                 spawnY = Random.Range(0f, screenHeight-1f);
    66.                 break;
    67.         }
    68.         Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(new Vector3(spawnX, spawnY, 0f));
    69.         lastActualEnemyCreated = Instantiate(enemyPrefab, spawnPosition, transform.rotation);
    70.         lastActualEnemyCreated.SetActive(true);
    71.     }
    72.  
    73.    
    74. }
    75.  
    76.  
     

    Attached Files: