Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    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.

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,371
    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.