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. Dismiss Notice

Question Argument Null Exception: Value cannot be null parameter name:Source

Discussion in 'Scripting' started by Mazespin, May 14, 2023.

  1. Mazespin

    Mazespin

    Joined:
    May 2, 2022
    Posts:
    3
    Code (CSharp):
    1.  
    2.  
    3. private List<charcter> _charactes;
    4.  
    5. public void ShowCharacter(CharacterName name,  CharacterMood mood)
    6.     {
    7.        
    8.         var character = _characters.FirstOrDefault(x => x.Name == name);
    9.         Debug.Log(name);
    10.  
    11.         if (character == null)
    12.         {
    13.             var characterObject = Instantiate(_characterPrefab, gameObject.transform, false); //instaniates the character if null
    14.             characterObject.name = name.ToString();
    15.             character = characterObject.GetComponent<charcter>();  
    16.             Debug.Log(character);
    17.             _characters.Add(character); //adds x character to the list
    18.  
    19.             Debug.Log(_characters[0]);
    20.         }
    Can someone explain why value cannot be null even if it was setup to and some some way around it. Sorry I am still quite a junior developer
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    There's so much misspelling and convention-breaking going on above I give up trying to figure out what is what.

    I see you have:

    charcter    <------- a type!! Make it uppercase first letter already!
    _charactes <- never initialized
    _characters <- never initialized
    character
    characterObject
    _characterPrefab


    Do yourself a favor, fix all the misspellings and choose some better more standard names for goodness sakes!!!

    Suggestions:

    Types:
    Character


    instances:
    characterInstance


    List of characters:
    PotentialCharacters
    or
    PossibleCharacters


    etc.

    Either way, the answer is still ALWAYS the same:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  3. Mazespin

    Mazespin

    Joined:
    May 2, 2022
    Posts:
    3
    Im aplogize but it's my first time submitting to a forum so i wasn't sure and left out some info which should probably be in
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Obviously you want to give us the same code you give to your computer, otherwise what's the point of your code when you can speak a language?