Search Unity

Creating a pop-up UI element

Discussion in 'Scripting' started by VhalirGames, Sep 4, 2015.

  1. VhalirGames

    VhalirGames

    Joined:
    Aug 4, 2015
    Posts:
    19
    I'm having a bit of trouble making a pop-up menu in a game I'm working on. I have a menu button in the corner of the screen, and when I click on it, I want a menu button to pop up with an animation that I did. I have the animation working just fine, and when I drag the pre-fab into the heirarchy under the canvas everything runs just fine, but my problem is coming from instantiating it with code. Here is the code I'm using

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class PopUpMenuScript : MonoBehaviour {
    6.  
    7.     public GameObject popUpMenuPrefab;
    8.  
    9.     // Use this for initialization
    10.     public void PopUpMenuInstantiator(Transform newParent){
    11.         GameObject popUpMenu = Instantiate (popUpMenuPrefab, new Vector3 (1f, 1f, 1f), Quaternion.identity) as GameObject;
    12.         popUpMenu.transform.SetParent(newParent, false);
    13.     }
    14.  
    15. }
    so I have the popUpMenuPrefab, and in the inspector I clicked on the PopUpMenuScript and dragged my prefab onto that variable, but whenever I run the game and click the button, it gives me this error

    UnassignedReferenceException: The variable popUpMenuPrefab of PopUpMenuScript has not been assigned.
    You probably need to assign the popUpMenuPrefab variable of the PopUpMenuScript script in the inspector.
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:60)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:80)
    PopUpMenuScript.PopUpMenuInstantiator (UnityEngine.Transform newParent) (at Assets/Scripts/PopUpMenuScript.cs:11)
    UnityEngine.Events.InvokableCall`1[UnityEngine.Transform].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:168)
    UnityEngine.Events.CachedInvokableCall`1[UnityEngine.Transform].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:286)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:602)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:744)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()

    That's rather long, so I think the most relevant part is this:

    UnassignedReferenceException: The variable popUpMenuPrefab of PopUpMenuScript has not been assigned.
    You probably need to assign the popUpMenuPrefab variable of the PopUpMenuScript script in the inspector.

    But I thought I did that by dragging my prefab into the spot for it on the script. I have the script being called from a button click.

    So, what do I need to change?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    are you assigning this on the PopUpMenuScript prefab in the project view? prefabs can't remember links to anything that isn't within the family of the prefab (i.e. the prefab's root gameobject of any descendant)
     
  3. VhalirGames

    VhalirGames

    Joined:
    Aug 4, 2015
    Posts:
    19
    Yes, that is what I am doing. Where should I assign it then?