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 wonde · Jul 02, 2015 at 03:44 PM · javascriptguigameobjectrenderer.enabled

Is it possible to start a game and get GUI-button act as like its already pressed? (Javascript)

Hello, I have a problem because I've created two GUI-buttons that hides/disables and unhides/enables gameObjects renderer. And I need to second one act like its already pressed once when the game starts or something like that.

The script I have:

 var S1 : GameObject;
 var S2 : GameObject;
 var S3 : GameObject;
 var S4 : GameObject;
 var K1 : GameObject;
 var K2 : GameObject;
 var K3 : GameObject;
 var K4 : GameObject;
 
 function OnGUI () {
     if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
     {
     S1.GetComponent.<Renderer>().enabled = false;
     S2.GetComponent.<Renderer>().enabled = false;
     S3.GetComponent.<Renderer>().enabled = false;
     S4.GetComponent.<Renderer>().enabled = false;
     K1.GetComponent.<Renderer>().enabled = true;
     K2.GetComponent.<Renderer>().enabled = true;
     K3.GetComponent.<Renderer>().enabled = true;
     K4.GetComponent.<Renderer>().enabled = true;
     }
     if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
     {
     S1.GetComponent.<Renderer>().enabled = true;
     S2.GetComponent.<Renderer>().enabled = true;
     S3.GetComponent.<Renderer>().enabled = true;
     S4.GetComponent.<Renderer>().enabled = true;
     K1.GetComponent.<Renderer>().enabled = false;
     K2.GetComponent.<Renderer>().enabled = false;
     K3.GetComponent.<Renderer>().enabled = false;
     K4.GetComponent.<Renderer>().enabled = false;
     }
  }

So is it possible to create something like that the "HOID2"-button already does the thing even it's never been pressed or is this a lost cause? Hope you understand what I mean, thanks in advance.

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
1
Best Answer

Answer by $$anonymous$$ · Jul 02, 2015 at 06:31 PM

Try this:

 #pragma strict
 
 var S1 : GameObject;
 var S2 : GameObject;
 var S3 : GameObject;
 var S4 : GameObject;
 var K1 : GameObject;
 var K2 : GameObject;
 var K3 : GameObject;
 var K4 : GameObject;
 
 function Start () {
     turnOneOnAndTheOtherOff(false);
 }
 
 function OnGUI () {
     if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
     {
         turnOneOnAndTheOtherOff(false);
     }
     if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
     {
         turnOneOnAndTheOtherOff(true);
     }
 }
 
 function turnOneOnAndTheOtherOff(value : boolean)
 {
     S1.GetComponent.<Renderer>().enabled = value;
     S2.GetComponent.<Renderer>().enabled = value;
     S3.GetComponent.<Renderer>().enabled = value;
     S4.GetComponent.<Renderer>().enabled = value;
     K1.GetComponent.<Renderer>().enabled = !value;
     K2.GetComponent.<Renderer>().enabled = !value;
     K3.GetComponent.<Renderer>().enabled = !value;
     K4.GetComponent.<Renderer>().enabled = !value;
 }


I've just tested it and works just fine

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Jul 02, 2015 at 06:38 PM 0
Share

The key to answer your question is to use the Start function provided by $$anonymous$$onoBehaviour: $$anonymous$$onoBehaviour.Start()

avatar image wonde · Jul 03, 2015 at 05:45 AM 1
Share

this works like a charm thank you @$$anonymous$$

avatar image sas_88 · Jul 03, 2015 at 06:11 AM 0
Share

Hi,

can u try this in the way,made some change on the code.Assign the gameobject to the related GameObject in the script.

 var S1 : GameObject;
  var S2 : GameObject;
  var S3 : GameObject;
  var S4 : GameObject;
  var $$anonymous$$1 : GameObject;
  var $$anonymous$$2 : GameObject;
  var $$anonymous$$3 : GameObject;
  var $$anonymous$$4 : GameObject;
  
  function Start () {
      turnOneOnAndTheOtherOff(false);
  }
  
  function OnGUI () {
      if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
      {
          turnOneOnAndTheOtherOff(false);
      }
      if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
      {
          turnOneOnAndTheOtherOff(true);
      }
  }
  
  function turnOneOnAndTheOtherOff(value : boolean)
  {
      S1.SetActive(value);
      S2.SetActive(value);
      S3.SetActive(value);
      S4.SetActive(value);
      $$anonymous$$1.SetActive(!value);
      $$anonymous$$2.SetActive(!value);
      $$anonymous$$3.SetActive(!value);
      $$anonymous$$4.SetActive(!value);
  }
avatar image $$anonymous$$ · Jul 03, 2015 at 12:09 PM 0
Share

Hey @wonde I'm glad it worked for you, care to accept my answer as the correct one? Thanks!

@sas_88 That's a better approach, yes, but maybe he only needed to turn the renderer off/on I tried not to mess too much with his code, only a little optimization in the repeating code side

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

2 People are following this question.

avatar image avatar image

Related Questions

Created game objects rollover show guy text 0 Answers

Trouble getting GUI phone to open/close 1 Answer

Enter Trigger, play animation(s) and sound FX 1 Answer

Setting Scroll View Width GUILayout 1 Answer

GUI GameObject Button? 1 Answer


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