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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Prefab not instantiating in the next scene

Discussion in 'Prefabs' started by BananaChipsLtd, Mar 29, 2023.

  1. BananaChipsLtd


    Mar 29, 2023

    I have a problem with bullet prefab in next scene. In the first scene the bullet is instantiated, but in other scenes the same bullet prefab is not instantiated for some reason. A picture of the situation is below.

    First, I reckoned it was due to the Biker was a regular scene asset (not a prefab). Initially I made a separate Biker gameobject for each scene. Then I made a prefab out of the Biker, but still the bullet is not instantiated. Everything else in the scene - like text labels, the player sprite itself and the gun - are displayed, only the bullet is not instantiated when I hit the space bar.

    So, even after making the player (Biker) a prefab, it still doesn't work. I debugged, and saw that in the first scene the method Shoot() is being called and the BulletTrailPrefab is instantiated, but as soon as I get to the second scene (using SceneManager) the Unity console doesn't add any new print() error messages, so I assume that the Shoot() code never gets loaded in the next scene.

    Any ideas? I would appreciate it very much if you could give some hints and tips as to what could possibly be the problem:) Perhaps that my Lives and Score text labels are not prefabs. So some things are prefabs - like enemies and player - but labels are not. Could that be an issue somehow?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    6. public class SwordRotation : MonoBehaviour
    7. {
    8.     public Transform BulletTrailPrefab;
    9.     public Transform BulletParticlePrefab;
    12.     void Shoot()
    13.     {
    14.        print("NEW BULLETTRAIL INSTANTIATED: " + BulletTrailPrefab);
    15.        if (BulletTrailPrefab != null)
    16.        {
    17.            Transform trail = Instantiate(BulletTrailPrefab,
    18.            firePoint.position, firePoint.rotation) as Transform;
    19.            LineRenderer lr = trail.GetComponent<LineRenderer>();
    21.            if (lr != null)
    22.            {
    23.                lr.SetPosition(0, firePoint.position);
    24.                lr.SetPosition(1, new Vector3(0, 40, 0));
    25.            }
    27.            Destroy(trail.gameObject, 0.3f);
    28.        }
    29.     }
    30. }
    33. public class CountDownTimer : MonoBehaviour
    34. {
    35.     void Update()
    36.     {
    37.         //...
    38.         Invoke("LoadNextScene", 6f);
    39.     }
    41.     void LoadNextScene()
    42.     {
    43.         activeScene = SceneManager.GetActiveScene();
    45.         if (activeScene.buildIndex == 2)
    46.             SceneManager.LoadScene(sceneNameToLoad);
    48.     }
    49. }