- Home /
Missing function in button on click
I'm totally a newbie of unity and scripting I made a C# script "ChangeScene" attached to game object"ChangeScene"
using UnityEngine;
using System.Collections;
public class ChangeScene : MonoBehaviour {
IEnumerator Wait(string scene,float wait)
{
yield return new WaitForSeconds (wait);
float fadeTime = GameObject.Find ("ChangeScene").GetComponent<FadingScript> ().BeginFade (1);
yield return new WaitForSeconds (fadeTime);
Application.LoadLevel (scene);
}
public void ChangeToScene(string scene,float wait)
{
StartCoroutine (Wait(scene,wait));
}
}
No error occurs but I can find the function"ChangeToScene" in button on click's list I picked ChangeScene(object)->ChangeScene(script), but there's no "ChangeToScene" to choose what's the problem?
It only appears if I remove the parameter of ChangeToScene but then I'll have to declare the variables as public variable outside and the input bar for them will be in the game object "ChangeScene" however, the function will be used by several buttons to change to different scenes I need to move the bar back to the button on click's list How can I do it?
P.S. I'm not sure if I've carelessly posted it twice
Answer by Cherno · Jul 19, 2015 at 07:45 PM
A function called by a button may only have one parameter at most; your ChangetoScene function has two (string and float). You can circumvent this limit by passing a custom class instance as a parameter which in turn contains all the required values, but you can not assign that function via the inspector; You will have to do this via script.
//This needs to be assigned via the inspector or via script if you wish
public Button button;
//Our class which will hold the needed values for a level change and which will get passed as single parameter
public class ButtonParameter {
public string scene;
public float wait;
//constructors
public ButtonParameter() {}
public ButtonParameter(string s, float f) {
scene = s;
wait = f;
}
}
void Start() {
//create a new instance of our custom ButtonParameter class
//"MyScene" and 1f are just sample values
ButtonParameter buttonParameter = new ButtonParameter ("MyScene", 1f);
//tell our button that it should call the ChangeToScene methid and pass it the buttonParameter instance created above
button.onClick.AddListener(delegate { ChangeToScene(buttonParameter ); });
}
IEnumerator Wait(string scene,float wait)
{
yield return new WaitForSeconds (wait);
float fadeTime = GameObject.Find ("ChangeScene").GetComponent<FadingScript> ().BeginFade (1);
yield return new WaitForSeconds (fadeTime);
Application.LoadLevel (scene);
}
//The ChangeToScene metod now accepts a single parameter, which is and instance of the ButtonParameter class
public void ChangeToScene(ButtonParameter bp)
{
//we can just access the variables and values of the ButtonParameter instance and pass it on to the Coroutine
//Alternatively, we could pass the ButtonParameter instance itself along so that the Coroutine accepts this type of parameter as well.
StartCoroutine (Wait(bp.scene,bp.wait));
}
error occurs at the first line
public Button button;
The type or namespace name `Button' could not be found.