Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Click on text in Canvas and go to a scene

Discussion in 'Scripting' started by shwa, May 16, 2019.

  1. shwa

    shwa

    Joined:
    Apr 9, 2012
    Posts:
    461
    Hi,

    Big fan of Unity. I work on the art and design side. Great respect for the code side.
    Thx in advance, as per below.

    - Simple Main Menu -

    Trying to figure out how i can click on a specific text field in the Canvas, and have that load a specific scene.
    I will have about 5 text fields that will take the player to distinct and separate scenes.

    void Update ()
    void Update()

    Does there need to be a space between Update and ()

    *Here's what i've come up with, by scouring the forums.
    Getting an error message:
    Assets/ZFBrowser/Demo/GSmed3.cs(21,11): error CS1513: } expected



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4. public class GSmed3 : MonoBehaviour {
    5.  
    6. void Update () {
    7.  
    8. if (Input.GetMouseButtonDown(0))
    9.          {
    10.              RaycastHit hit;
    11.              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.              if (Physics.Raycast(ray, out hit, 100.0f))
    13.              {
    14.                  //Replace this with whatever logic you want to use to validate the objects you want to click on
    15.                  if(hit.collider.gameObject.tag == "Clickable")
    16.                  {
    17.                      SceneManager.LoadScene("BRxMeditation1");
    18.                  }
    19.              }
    20.          }
    21.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    There is no need for the space between Update and (), nor does it matter if there is one.

    You're missing a last } at the end to close the class off. Just count the number of { and } and you should have the same amount.
     
  3. shwa

    shwa

    Joined:
    Apr 9, 2012
    Posts:
    461
    Thanks Brathnann.
    That solved the error issue.

    I've dropped the fixed script on the text object, and put a box collider 2D on it, and set it to trigger the script.

    I'm now getting this error message:
    NullReferenceException: Object reference not set to an instance of an object
    GSmed5.Update () (at Assets/aaa Go Scene CPlusScriptsFolder/GSmed5.cs:12)

    Any idea of what i'm doing incorrectly?
    Thx.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    The line that is pointing at doesn't make sense, so I assume your script was modified. It may be that Camera.main is null. Make sure you have a camera tagged as MainCamera in your scene.

    Also note you should cache the Camera.main value as it, as Camera.main basically is a GameObject.Find call.

    All that being said, why didn't you just make these buttons with an invisible image so your text showed, but the button itself doesn't?