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

Bug Instantiate() wont work

Discussion in 'Scripting' started by ItsMe1423, Apr 3, 2024.

  1. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I am trying to duplicate a player menu in this chunk of code
    Code (CSharp):
    1.         GameObject MakeSpecificMenu = Instantiate(playerMenu);
    2.         RectTransform MSMrt = MakeSpecificMenu.GetComponent<RectTransform>();
    3.         MakeSpecificMenu.transform.SetParent(playerMenu.transform.parent);
    4.         MakeSpecificMenu.name = tempPlayer.Name + " menu";
    5.         Debug.Log(MakeSpecificMenu.name);
    6.         MSMrt.anchoredPosition = new Vector2(500, 50);
    7.         MSMrt.localScale = new Vector3(1, 1, 1);
    This is the log:
    upload_2024-4-3_20-10-57.png
    The errors appear after I try to open it because it can't be found and that it because for some reason it doesn't exist.
     
  2. ChaosEaterY

    ChaosEaterY

    Joined:
    Jun 3, 2018
    Posts:
    5
    I assume you did not set playerMenu in your inspector
     
  3. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have set it
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    Let's not argue (again) about null references.

    If you think you set it, you didn't.

    If you DID set it, then something ELSE is null.

    It's not a matter of debate, it's not a matter of disagreement.

    Go fix it. Start at Step #1. Do NOT start at any other step. Here's how:

    The answer is always the same... ALWAYS!

    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
     
  5. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    fixed it I got confused with the names
     
  6. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I set the name to the wrong object because I copy-pasted it and forgot to switch variables this is in a part I didn't attach so there was no way you could help me with that
    but thank u both for trying