• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Carbon Monoxide · Jul 19, 2015 at 07:00 PM · c#uibuttonfunction

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

Comment
Tunity

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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));
 }
Comment
XeeMee

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Carbon Monoxide · Jul 20, 2015 at 07:50 AM 0
Share

error occurs at the first line

 public Button button;

The type or namespace name `Button' could not be found.

avatar image saschandroid · Jul 20, 2015 at 07:56 AM 0
Share

Add:

  using UnityEngine.UI;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why will my button not let me run a function? 4 Answers

Change button on focus 0 Answers

Changing anchor positions on UI? 0 Answers

Help making multiple buttons do a specific set of actions in any order pressed 1 Answer

How do I call an DontDestroyOnLoad function on UI text 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges