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 jpvanmuijen · Apr 24, 2013 at 12:29 PM · c#getcomponent

Re-run Start() and OnGUI() from another script

Hi all,

Is there a way to re-run a script's Start() and OnGUI() functions from another script?

I'm trying to refresh OnGUI with a new variable, like so;

 // Script A, attached to object 1
 public GameObject object2; // Link to object 2 via inspector
 public string variableABC; // Set in inspector
 
 void OnClick () { // Externally defined function
         object2.GetComponent<ScriptB>().variableXYZ = variableABC; // Sends value to script B
         // Somehow trigger Script B Start() and OnGUI() from here
     }

 // Script B, attached to object 2
 public string variableXYZ; // Set by script A
 
 void Start () {
         doSomething(using-variableXYZ);
     }
 void OnGUI () {
         drawSomething(using-variableXYZ);
     }

The value for variableXYZ gets set no problem, but it's not used in the script since it already ran at the start.

I've tried setting Start and OnGUI to public and calling object2.GetComponent< scriptB >().Start(); from script A, which has no effect.

Any help would be appreciated!

Thanks

Comment
Add comment · Show 3
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 jpvanmuijen · Apr 24, 2013 at 12:57 PM 0
Share

Ok, I managed to re-run Start using this script;

 ScriptB foo = object2.GetComponent<scriptB>();
 foo.Start();

And I just found out OnGUI is an ongoing function (duh), so it should just redraw with the new variable. And it does appear when I debug log the new value for variableXYZ.

However, OnGUI doesn't really seem to update its contents. I'm using uWeb$$anonymous$$it to draw a browser window, using this script;

     public UW$$anonymous$$View View;
     public string URL;

     public void Start ()
     {
         View = UW$$anonymous$$Core.CreateView ("WebGUI", URL, Screen.width, Screen.height);
     } 
     
     public void OnGUI ()
     {
         View.OnWebGUI(0, 0);
     }

Where URL is the variableXYZ from my previous post.

So I'm guessing it has something to do with one of these lines not updating.

Is this even something which can be fixed within this script? Or would it be something tucked away in the uWeb$$anonymous$$it source code. In which case, I should probably head over to their support forums.

Thanks again

avatar image LyanApps · Apr 24, 2013 at 01:12 PM 0
Share

Because Start and OnGUI have specific roles in Unity, I would really suggest putting your code into a separate function and calling that ins$$anonymous$$d. Also you need to make the function public to be accessed from an alternate script.

 int XYZ
 public void setXYZ(int value){
  XYZ = value;
 }
 
 void Start(){
  setXYZ(10);
 }
 
 ...
 
 ScriptB foo = object2.GetComponent<scriptB>();
 foo.setXYZ(5);
avatar image jpvanmuijen · Apr 24, 2013 at 01:26 PM 0
Share

Thanks for your comment, I moved my code to a separate function as you suggested.

As you can see, I narrowed the issue down to OnGUI not drawing a new uWeb$$anonymous$$it window. Which (unfortunately) seems to needed to make this work.

I don't expect this to be a UW$$anonymous$$ support forum, but maybe there's a way around OnGUI? I.e. executing this line of code some alternative way?

0 Replies

· Add your reply
  • Sort: 

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

12 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

Related Questions

How am I supposed to use GetComponent? 1 Answer

How Activating a boolean fron another script(C#) 1 Answer

How to use "GetComponent" and "transform" code in dll file? 0 Answers

I cant get script of a GameObject(Camera) 6 Answers

C# GetComponent Issue 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