So I’m new with Unity, and I am making a 2D platformer. My intention is when my player, let’s call it MyPlayer triggeres some game object Canvas should appear with question and possible answers (a,b,c,d). Anyway when MyPlayer enters the game object it says: You probably need to assign the myCanvas variable of the PickUp_Object script in the inspector. Where myCanvas is the canvas I’m trying to use and PickUp_Object is the object which is triggered.
Here’s the code:
public Canvas myCanvas;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == “Player”)
{
//Time.timeScale = 0; //pausing the game
Destroy(GameObject.FindWithTag(“BlockForDestroy”)); //Destroys the block so Player can go on with the level
myCanvas.enabled = true;
}
}
I think I get what you are trying to do.
First you don’t need to disable the canvas.
In the canvas create a new panel (this will be the dialog you hide/unhide), on the new panel create other UI element like text, or button etc.
Now create a new script make a public variable e.g
public class Dialogs
{
public GameObject DialogPanel;
}
attach this to the Canvas GameObject, Now drag the gameObject that is the Panel over the DialogPanel Name.
Now we need access to the Dialogs script from some sort of main script, not sure how you have your scene setup.
Private Dialog mDialog;
void Awake()
{
GameObject mTemp = GameObject.Find("Canvas");
mDialog = mTemp.getcomponent<Dialog>();
}
now in your game just do mDialog.gameobject.setactive( false or true);
There are a few other ways to do it but I hope this helps.
Yes I’ve done similar thing, but when I try to setactive(false or true) it says that Object reference not set to an instance of an object. So I tried to make public variable for my panel but now it says “CreateGameObject is not allowed to be called from a MonoBehaviour constructor”. And I tried to set it in Start() or Awake(), doesn’t work either.
Can you post your updated code?