- Home /
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
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
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);
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?
Your answer
Follow this Question
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