Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Keyojin · Feb 19, 2014 at 03:21 AM · c#menu

multi scripts 1 action the same in each. C#

OK i'll try to explain this the best i can. I have have a main menu that has : New Game, Load Game, Options, and Exit. The Main Menu, New Game, Load Game, and Option are cameras with their own scripts for their GUI's. They all use these var to reference each other.

 //Back Button Varabels
     public GameObject MMCamra;
     public GameObject OPCamra;
     public GameObject OPCamra2;
     public GameObject OPCamra3;

Ops, New, and Load; all have a back button that does this.

 Debug.Log("Switching to Main Menu");
 MMCamra.gameObject.SetActive(true);
 OPCamra.gameObject.SetActive(false);
 OPCamra2.gameObject.SetActive(false);
 OPCamra3.gameObject.SetActive(false);

So instead of using these lines of code on each script. i'm trying use one script on empty gameobject to do the above code. I hope i'm making scene here.

Comment
Add comment
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
0
Best Answer

Answer by iHaveReturnd · Feb 19, 2014 at 03:26 AM

You could just put that set of code into a function like:

 public void GoBack(){
 Debug.Log("Switching to Main Menu");
 MMCamra.gameObject.SetActive(true);
 OPCamra.gameObject.SetActive(false);
 OPCamra2.gameObject.SetActive(false);
 OPCamra3.gameObject.SetActive(false);
 }

And call that function whenever any of those back buttons is hit. I hope I understood you question right but let me know if thats not what you meant

Comment
Add comment · Show 6 · 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 Keyojin · Feb 20, 2014 at 01:21 AM 0
Share

I'm tried that and got this massage.

Assets/Scripts/$$anonymous$$enuScripts/NewGame.cs(175,25): error CS0103: The name `GoBack' does not exist in the current context

avatar image iHaveReturnd · Feb 20, 2014 at 02:33 PM 0
Share

How are you calling the function? And are you trying to call it from another script?

If NewGame.cs is a separate script, you'll have to reference the script it's in. So for example (if its in main menu):

public $$anonymous$$ain$$anonymous$$enu main$$anonymous$$enuScript; //declare a var to drag the script onto

//insert conditions for when GoBack should be called, such as button down etc main$$anonymous$$enuScript.GoBack();

avatar image Keyojin · Feb 22, 2014 at 01:43 PM 0
Share

Yes im trying to call a function from another script which is on an empty gameObject. The gameObject is called $$anonymous$$anager and has the Button$$anonymous$$anager script on it. Which set which camera is active. im using,

 public gameObject $$anonymous$$anage;

To reference the $$anonymous$$anager.

 using UnityEngine;
 using System.Collections;
 
 public class Button$$anonymous$$anagerScript: $$anonymous$$onoBehaviour {
 
     //Button Var
     public GameObject $$anonymous$$$$anonymous$$Camra;
     public GameObject OPCamra;
     public GameObject OPCamra2;
     public GameObject OPCamra3;
 
     
     public void GoBack()
     {
         Debug.Log("Switching to $$anonymous$$ain $$anonymous$$enu");
         $$anonymous$$$$anonymous$$Camra.gameObject.SetActive(true);
         OPCamra.gameObject.SetActive(false);
         OPCamra2.gameObject.SetActive(false);
         OPCamra3.gameObject.SetActive(false);
     }
     public void GoNewGame()
     {
         Debug.Log("Switching to New Game");
         $$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
         OPCamra.gameObject.SetActive(false);
         OPCamra2.gameObject.SetActive(true);
         OPCamra3.gameObject.SetActive(false);
     }
     public void GoLoadGame()
     {
         Debug.Log("Switching to Load Game");
         $$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
         OPCamra.gameObject.SetActive(false);
         OPCamra2.gameObject.SetActive(false);
         OPCamra3.gameObject.SetActive(true);
     }
     public void GoOptions()
     {
         Debug.Log("Switching to Options");
         $$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
         OPCamra.gameObject.SetActive(true);
         OPCamra2.gameObject.SetActive(false);
         OPCamra3.gameObject.SetActive(false);
     }
 
 }


Here is the button im useing.

 if (GUI.Button(new Rect(Screen.width * .01f, Screen.height * .9f, Screen.width * .1f, 25),new GUIContent( "Back" , "Back\n\n Go back to the $$anonymous$$ain $$anonymous$$enu")))
         {
         GoBack();
         }
avatar image iHaveReturnd · Mar 01, 2014 at 02:32 AM 0
Share

You need to get a reference to your Button$$anonymous$$anagerScript for the button.

Right now it would be trying to call its own "GoBack" function, you can do something like this in the button's script:

 public Button$$anonymous$$anagerScript button$$anonymous$$anager; //drag the manager's script in the inspector here
 
 void Update(){
 if (GUI.Button(new Rect(Screen.width * .01f, Screen.height * .9f, Screen.width * .1f, 25),new GUIContent( "Back" , "Back\n\n Go back to the $$anonymous$$ain $$anonymous$$enu")))
        {
        button$$anonymous$$anager.GoBack();
        }

}

And that should do it. (If I'm reading the issue correctly)

avatar image Keyojin · Mar 01, 2014 at 03:31 AM 0
Share

Yes that was it. Thank you vary much. i can't believe i was having so much trouble with something so simple. thank you again.

Show more comments

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to make a menu like ironpants 0 Answers

c# how to get to idle from walk 0 Answers

Alarm lights not changing 1 Answer

What is way to flash the screen WITHOUT using a Texture? 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