Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

There's something wrong with scene changing

Discussion in 'Scripting' started by Isabelle_Melies, Jun 26, 2014.

  1. Isabelle_Melies

    Isabelle_Melies

    Joined:
    Jun 8, 2014
    Posts:
    21
    Greetings, I have problem with my application. I made button that should give me sound effect when click it, but when i run it this button bring me to another scene. How could this be ? this below is my script , no scene script in my codes

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class ExampleBehaviourScript : MonoBehaviour
    5. {
    6.     public AudioClip clip;
    7.  
    8.     void start()
    9.     {
    10.         gameObject.renderer.material.color = Color.yellow;
    11.         gameObject.SetActive(false);
    12.     }
    13.     void OnMouseDown(){
    14.         AudioSource.PlayClipAtPoint (clip, this.transform.position);
    15.     }
    16.  
    17.  
    18.     void Update ()
    19.  
    20.     {
    21.                    
    22.                        
    23.     }
    24. }
    25.  
     

    Attached Files:

  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Is there another script in your scene that is also handling OnMouseXXX() or input in OnGUI()?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    NomadKing likes this.
  4. Isabelle_Melies

    Isabelle_Melies

    Joined:
    Jun 8, 2014
    Posts:
    21
    yes there's another script. But is has fewer codes i used it on game object. I just copied example.cs to my 6 yelllow buttons(example.cs). Here's below is braille.cs this script is used for game object.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Braille : MonoBehaviour {
    5.     public AudioClip clip;
    6.     // Use this for initialization
    7.     void Start () {
    8.         AudioSource.PlayClipAtPoint (clip, this.transform.position);
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.     }
    15. }
    16.  
    here's my expectation
    here's below is green button script, and there's scene changing script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class menu : MonoBehaviour {
    5.     public AudioClip clip;
    6.     // Use this for initialization
    7.     void OnMouseDown () {
    8.         Application.LoadLevel("scene2");
    9.         AudioSource.PlayClipAtPoint (clip, this.transform.position);
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16. }
    17.  
    you can see my game object configuration in my screenshot below

    THOSE ALL BUTTONS ARE MADE FROM PICTURE FILES
     

    Attached Files:

    Last edited: Jun 27, 2014
  5. Isabelle_Melies

    Isabelle_Melies

    Joined:
    Jun 8, 2014
    Posts:
    21
    can I make it on one script ?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    If you use OnGUI(), you can put all the buttons in one script. Read the GUI Scripting Guide for instructions.

    If you want to use OnMouseDown(), they have to be separate scripts. This is because OnMouseDown works with the object's Collider or GUIElement.

    It certainly appears that the green button script's OnMouseDown() method is being called. You can add a Debug.Log() line to each script's OnMouseDown() to make it easier to see when each button think it's being clicked:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class menu : MonoBehaviour {
    5.     public AudioClip clip;
    6.     // Use this for initialization
    7.     void OnMouseDown () {
    8.         Debug.Log("menu.OnMouseDown() called on " + name); // <-- ADD THIS
    9.         Application.LoadLevel("scene2");
    10.         AudioSource.PlayClipAtPoint (clip, this.transform.position);
    11.     }
    12. ...