Search Unity

CS0649: Prefab is never assigned to [...] default value null

Discussion in 'Prefabs' started by Lorico1000, Oct 5, 2020.

  1. Lorico1000

    Lorico1000

    Joined:
    Oct 5, 2020
    Posts:
    1
    Hi,

    I am completely new to Unity and C# in general, and I am currently taking the online course from Jon Weinberger (Ultimate Guide to Game Development with Unity).

    In the making of a small space shooter game, I am currently at the stage of learning to spawn (Instantiate) lasers (the prefab here) but as I'm following the exact same steps as he is, I get an error message:

    warning CS0649: Fiel 'Player._laserPrefab' is never assigned to, and will always have its default value null

    I don't see why it is not assigned, since I have:

    - private Gameobject _laserPrefab; (serialized, and located in public class Player : MonoBehaviour)

    - if (Input.GetKeyDown(KeyCode.Space))
    {
    Instantiate(_laserPrefab);
    }
    (located in void Update() )

    and inside of Unity, my Laser object is indeed a Prefab (blue cube icon), that I dragged onto one of the Player variables called "Laser Prefab"

    Although it's definitely basic stuff, I have no clue what it could come from since I am following the teacher's steps, so if anyone happened to have followed the same course, and could help me out here, it would mean a lot.

    Thanks!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Usually the reason for that exact bug is that you have more than one Player around, and the other one doesn't have the thing assigned.

    If you search for "t:player" in the hierarchy, you'll be able to see everything in your scene that has the player script attached to it - there's probably more than one.

    Alternatively, if you click the error message, the object containing the script with the error should be pinged.