Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Prefab not instantiating in the next scene

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

  1. BananaChipsLtd

    BananaChipsLtd

    Joined:
    Mar 29, 2023
    Posts:
    1
    Hey,

    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;
    5.  
    6. public class SwordRotation : MonoBehaviour
    7. {
    8.     public Transform BulletTrailPrefab;
    9.     public Transform BulletParticlePrefab;
    10.  
    11.  
    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>();
    20.  
    21.            if (lr != null)
    22.            {
    23.                lr.SetPosition(0, firePoint.position);
    24.                lr.SetPosition(1, new Vector3(0, 40, 0));
    25.            }
    26.  
    27.            Destroy(trail.gameObject, 0.3f);
    28.        }
    29.     }
    30. }
    31.  
    32.  
    33. public class CountDownTimer : MonoBehaviour
    34. {
    35.     void Update()
    36.     {
    37.         //...
    38.         Invoke("LoadNextScene", 6f);
    39.     }
    40.  
    41.     void LoadNextScene()
    42.     {
    43.         activeScene = SceneManager.GetActiveScene();
    44.      
    45.         if (activeScene.buildIndex == 2)
    46.             SceneManager.LoadScene(sceneNameToLoad);
    47.      
    48.     }
    49. }
    PrefabInstantiationIssue2.PNG