Hey!
im making a simple game for a college project that is focused around dialogue - i was hoping to have questions displayed on a canvas as text with a choice of buttons as a reply, this way when a button is pressed it could make a different canvas appear containing the reply and more options, problem is that i have no idea how to do this - i know that i will need to assign a C# script to the button as i have done this before but i dont know what elements of code i would need to use.
is there a command simple command that i can use to do this?
I have very little coding experience as i only started learning C# in september
Okay dokies so I went ahead and messed with it a little for you and manage to figure it out.
using UnityEngine;
using System.Collections;
public class Disable : MonoBehaviour {
public Canvas myCanvas;
// create another canvas if you use more than 1
public void DisableCanvas(){
myCanvas.enabled = false; //Set to true if you want to enable
Debug.Log("Clicked");
}
// Create another public void for the 2nd canvas
}
For this you need to create a variable for the canvas then add the canvas which you want to disable to the script via the canvas you attached it to… too many canvasssss okay after that disable as shown in the script bu checking is it enabled and should it be enable etc etc.
From there go to your button add the canvas with the script attached and choose the disable canvas script.
P.S: My instructions suck but hopefully you will be able to understand what im getting at.
PPS: I actually just read the request facepalm gimme a sec to make it a bit more yay for what you want to do
okay too much spam on my previous comment Are you going to use than one canvas for this? If so you can use the same script but just make more variables and add them to the appropriate buttons so they disable and enable the appropriate canvases. I will update the script for you.
Okay so i went and attached this script to the button that i wanted to click to disable the canvas and it didn’t work, i got no errors, i just didnt work - the canvas did not disable - any ideas as to why? thank you again for the help by the way
The script has to go on the canvas itself, after you add it to the canvas you will see you can add a target to the script so add your target canvas there.
This is the Canvas where I have added the script and under the script the Canvas which I want enabled or disabled:
Now go to your button and add another OnClick function choose Runtime and add the canvas which has the script attached to it. Where it says No Function Click it and go down to whatever you called the script eg, Disable and then select DisableCanvas.
This is what the button should look like in the end:
Hope this helps and yes I know my epic paint skills is well… epic
That worked great, thank you so much - you’re amazing and your epic paint skills definitely are epic haha!
there is one last thing that i would like to ask which is a bit different than the previous question; i tried using the OnTriggerEnter function to have the canvas appear when i entered a trigger but i couldn’t get it to work, the canvas didnt seem to enable itself when i entered the trigger, do you have any idea as to if i need to use a different function to do this as the canvas is part of the GUI rather than an object?