Turn off toggle box and graphic when game starts - c#

So this should be a simple answer but I cannot seem to find it anywhere. In Unity, I have a toggle box I want to turn off when the game starts, but unlike most game objects or text, SetActive does not work with toggle boxes. What is the command to turn the graphic off?
public void StartGame () {
mainText.SetActive (false);
startButton.SetActive (false);
StartCoroutine (SpawnBalls ());
//Turn off toggle box graphic
playing = true;
}
public void AddBall () {
if (ballBox.isOn) {
ballNumber = 3;
//Debug.Log ("yes");
} else {
ballNumber = 2;
}

If I understand you correctly you want to be able to remove the display of the toggle on the start of the game? If so set a reference to the toggle you want to click on and off heres a small example:
Toggle T = GameObject.Find("myToggle").GetComponent<Toggle>();
T.gameObject.SetActive(false);
Make a reference to it and you should be able to click it of and on as you need it tested it out on a game I'm working and it worked for me let me.

Related

Unity - Panel is disabled and re-enabled immediately afterwards (C#)

Just need a way to get around this problem. I know what the issue is, I just need a viable solution.
So basically I have a script called MouseManager, which detects if you're looking at a specific game object and if you press "f" it tells another script, called UIManager, to open the GameObject's panel (I have a game where you can press f on a console and it enables a UI with a puzzle).
Basically, I want the user to be able to press "f" or "esc" and have it exit the panel, however if you press "f", it disables the panel but instantly re-enables it as you're still looking at the gameobject, and both checks are in Update()
I need a workaround, if anyone knows one PLEASE comment as this issue is getting on my nerves :P
Thanks
EDIT: By console I mean there's a GameObject that looks like a console.
EDIT 2: Here's the code, I'll put some comments in it so you can understand what each thing does. I will be refactoring it soon because it's messy...
void Start()
{
uiPuzzles = GameObject.Find("PuzzlesUI").GetComponentInChildren<UIManager>(); // Currently I have the UIManager set to a canvas called PuzzlesUI. I will soon be moving it to an empty GameObject.
}
void Update()
{
if (!uiPuzzles.invActive && !uiPuzzles.puzzleActive) // invActive and puzzleActive are two public booleans that just show if the inventory or any puzzle panel is empty (UIManager handles opening and closing the inventory too)
{
if (Input.GetButtonDown("Use") && !uiPuzzles.invActive && !uiPuzzles.puzzleActive) // Use key is bound to "f". Currently Im just checking if puzzleActive and invActive are still false.
{
ray = GetComponentInChildren<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 7)) // Raycast where you're looking
{
uiPuzzles.openPuzzleOverlay(hit.collider.name); // function in UIManager which will check if the hit collider is in a list of all the puzzle colliders currently added and if it is, open that panel.
}
}
}
}
As for UIManager:
public void openPuzzleOverlay(string collider) // Get a panel name
{
foreach (Puzzle item in puzzleList)
{
item.panelGameObject.SetActive(false); // Disable all puzzle panels.
if (item.consoleGameObject.name == collider) // unless its the given panel,
{
item.panelGameObject.SetActive(true); // then enable it.
Cursor.lockState = CursorLockMode.None; // Unlock the mouse.
Cursor.visible = true; // Show the mouse.
DisablePlayerUI(); // Disable the UI (e.g. crosshair)
puzzleActive = true; // Disable movement. (Because player movement requires puzzleActive and invActive to also be false
}
}
}
void Update ()
{
if (Input.GetButtonDown("Use") && puzzleActive && !invActive && // If you want to use a console and the puzzle is already active
!puzzleList.Where(p => p.panelGameObject.activeSelf).FirstOrDefault().panelGameObject.GetComponentInChildren<InputField>().isFocused) // Check if the currently active panel's input field is not focused.
{
ClosePuzzleOverlay(); // Close the puzzle.
EnablePlayerUI(); // Enable the UI.
}
}
public void ClosePuzzleOverlay()
{
foreach (Puzzle item in puzzleList)
item.panelGameObject.SetActive(false);
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
puzzleActive = false;
}
The problem is being caused by the two input checks. When F is pressed both are true so the UI is being closed and then opened in the same frame.
Take the Input code out of UIManager and keep it in one place. Then you can rework your input class to be something like this:
//MouseManager Update
if(Input.GetButtonDown("Use")){
if(uiPuzzles.isActive)
uiPuzzles.Hide();
else
uiPuzzles.Show(GetPuzzleName());
}
Now it will only call either open or close on the frame which F is pressed down.

Start a movie on click

I have been having difficulty getting my movie to start playing once a certain button is hit. I can get it to work with void Start() but then the video starts playing as soon as the scene is loaded which I don't want. I have tried this for when the button is pressed to start playing the movie.
public MovieTexture myMovie;
public RawImage myLayer;
private new AudioSource audio;
public void onClick()
{
GetComponent<RawImage>().texture = myMovie as MovieTexture;
audio = GetComponent<AudioSource> ();
audio.clip = myMovie.audioClip;
myMovie.Play ();
audio.Play ();
}
The movie will show but not actually play. I'm sure I'm missing something obvious but as I am a noob at coding I can't figure out what it is. Any help would be greatly appreciated!
Make a custom method which will contain
void Play()
{
myMovie.Play ();
audio.Play ();
}
and assign it to the button component in the inspector
EDIT:
I meant, don't use onClick as you use Start etc, it's not executable like that. There are 2 ways:
1) You make a custom method (called whetever you want) e.g as I listed above and assign it to the onClick field in the inspector, press +, add a script and select a method from that script;
2)
public Button yourButton;
void Start () {
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
Debug.Log ("You have clicked the button!");
}
like in the reference: docs.unity3d.com/ScriptReference/UI.Button-onClick.html
You can add a Video Player using Create -> Video -> Video Player
Set video player video clip, Render Mode as Camera Far Plane and Select camera as Main Camera
Create script as given below. Just call the playMovie() method in any button event

How to create a dialogue box and display it when an object in clicked in unity 3d?

I am relatively new to scripting and unity. How can i create a dialog box with "Yes" and "No" buttons and display it when a particular objects in unity 3d is clicked ?
Keep the Dialogue Text UI object and Button UI objects as children of an empty Rect/UI object.
Use some code like below in the object that will be clicked:
void OnMouseDown()
{
dialogueBoxUIObject.SetActive(true);
}
Keep the dialogueBoxUIObject a public GameObject so that you can assign it in inspector. Keep Collider and Rigidbody components with no gravity with the object you want to click on.
If you're heading for the scripting approach, use OnMouseDown!
boolean isOpen = false;
void OnGUI() //I think this must be used on the camera so you may have to reference a gui controller on the camera
{
if(isOpen) //Is it Open?
{
if(GUI.Button(new Rect(10, 10, 100, 50), "Yes")) //Display and use the Yes button
{
Debug.Log("Yes");
isOpen = false;
}
if(GUI.Button(new Rect(110, 10, 100, 50), "No")) //Display and use the No button
{
Debug.Log("No");
isOpen = false;
}
}
}
void OnMouseDown() //Get the mouse click
{
isOpen = true; //Set the buttons to appear
}
There's a good Unity tutorial on how to achieve a modal box here:
https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window
It achieves what you need to do and also adds other components that might help you in the future. You can see all the code that's used for achieving the dialog box below the video.
You can use a Canvas Group to group inside it the dialog you want and both Yes/No buttons and set its Alpha to 0 and Interactable to false, so it doesn't show up initially.
Then attach a script to the object you want to click and use OnMouseUp and inside it change the CanvasGroup Alpha to 1 and Interactable to True.

Reverse animation in unity 3D

I have an FBX object with animation. The object is a box with animation for opening. What I'm trying to do is when the user clicks a button the box will open (play open animation) and when the button is clicked again the box will close (play open animation backwards).
While the opening animation is playing and I click the button again the opening animation stops and the box starts to close, that works fine.
The problem is that when the animation is finished (open) and then I click the button to close, the animation is not playing and it just jumps to a closed box without animation.
Here is my code:
public class ClickBtn : MonoBehaviour {
public GameObject box = null;
bool reverse = false;
private void OnMouseDown()
{
Debug.Log(reverse);
if (!reverse)
{
box.animation["Take 001"].speed = 1;
}
else
{
box.animation["Take 001"].speed = -1;
}
reverse = !reverse;
box.animation.Play("Take 001");
}
}
your animation.WrapMode is set wrong (probably WrapMode.Once, which is default). In your case you could use:
WrapMode.PingPong: Ping Pong's back and forth between beginning and end.
animation.wrapMode = WrapMode.PingPong;
Mind you, you don't need
box.animation["Take 001"].speed = -1;
anymore, this is done automatically.
When animation is finished its time is reseted to beginning.
Simple workaround set time to end before playing backwards.
public GameObject box;
bool direction = false;
private void OnMouseDown()
{
Debug.Log(direction);
if (!direction)
{
box.animation["Take 001"].speed = 1;
}
else
{
box.animation["Take 001"].speed = -1;
//if animation already finisihed, set time to end before playing it backwards
if(!box.animation.isPlaying){
box.animation["Take 001"].time =box.animation["Take 001"].clip.length;
}
}
direction = !direction;
box.animation.Play("Take 001");
}

Switch between multiple materials on a button press

I need to have an object in my scene change between two different materials at run time, when ever a button is pressed in my Unity project. However, I have never done this before and I'm having an issue getting my head around how to do this.
In my scene I have one game object I've called my controller. This script holds my material switching class and is looking like this:
public GameObject cupMesh;
bool isOn = true;
// Use this for initialization
void Start ()
{
cupMesh = GameObject.Find("CupMesh");
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
if(GUI.Button(new Rect(10,10, 100, 40), "Show mesh"))
{
renderer.enabled = false;
}
}
I know this doesn't change the material, but the above code does nothing. I've never modified anything on the mesh renderer before but I know there is a list of materials on it.
How can I access that list so I can have my program switch between the two materials found there?
To show or hide a gameObject, rather than using render.enabled property, you should use this:
// Unactivates the game object.
gameObject.SetActive (false);
However it's not clear from the code if you want to adjust the material of the object the script is attached to, or the cupMesh game object.
If you wanted to make the cupMesh disappear, you would use:
cupMesh.SetActive (false);
Or if you wanted to access the material component of the cupMesh, this is
cupMesh.renderer.material

Categories

Resources