The question is answered, right answer was accepted
Mouse Active Once Canvas Active, Help!
Hello, I am trying to make a script so the mouse is active once a canvas is active in game. I am unsure on how to make it work and what I currently have throws errors. Here is my script
using UnityEngine;
using System.Collections;
public class MouseGone : MonoBehaviour
{
// Use this for initialization
void Start()
{
//Set Cursor to not be invisible
Cursor.visible = false;
if (Canvas.gameObject = false);
}
void update ()
{
//Set Cursor to be visible
Cursor.visible = true;
if (Canvas.gameObject = true);
}
}
If you can help me in anyway that will be greatly appericated!
Answer by pauldoody25 · Nov 30, 2017 at 04:47 AM
So several things.
-update needs to be capitalized for the built in function. -if statements come before the result you want to execute. -Canvas.gameObject is invalid. You need to reference a specific canvas object -gameObject isn't a boolean, so you can't compare that to true or false.
Also if I understand what you're trying to do, you should have everything in the Update function. So you should have:
using UnityEngine;
using System.Collections;
public class MouseGone : MonoBehaviour
{
Canvas canvas; //assign this by dragging your canvas to it in the inspector
void Start()
{
Cursor.visible = true;
}
void Update ()
{
Cursor.visible = (canvas.gameObject.active);
}
}
Hope that helps
I am having an error once play is pressed! I am also getting a warning in the code on the same line. alt text
Never $$anonymous$$d it works now. Just had to add 'public' before line 5 as i couldn't drag anything in otherwise. Thank you very much! :)
Follow this Question
Related Questions
Event on mouse up 2 Answers
Mouse-Keyboard Not Working - New User 1 Answer
Needing help with 3rd person mouse camera 1 Answer
Get coordinates of mouse-click on plane 1 Answer
Player doesn't turn 1 Answer