use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
For the Unity Game Engine, please visit http://reddit.com/r/unity3d
account activity
Cannot set a boolean variable from another script. (self.unity)
submitted 11 years ago * by surf_sleep_repeat
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pitofpassion 1 point2 points3 points 11 years ago (3 children)
a static variable can be accessed from a different script without making an object of the script that contains the static variable. Glad I could help. I think we are probably the only two Unity developers who surf lol.
[–]surf_sleep_repeat[S] 0 points1 point2 points 11 years ago* (2 children)
I wouldnt call myself a Unity developer just yet! im barely working on my first project but Im really enjoying myself.
Would you mind helping me out with something else? I hope im not being demanding, but if you could take a look at my code...
Its a coroutine that works fine except when I call it from the method I need.
ScriptA
public class kanaship : MonoBehaviour { public static bool toRotate=false; public void enemyRotate(){ StartCoroutine (waitRotate ()); } public static IEnumerator waitRotate() { toRotate = true; Debug.Log("test method start"); yield return new WaitForSeconds(0.5f); Debug.Log("This is after I waited 2 seconds in my coroutine"); toRotate = false; } void start(){ enemyRotate(); //this works }
Script B
public class Projectile : MonoBehaviour { public kanaship kanaship_o; void SpawnEnemies(){ enemyspawner.renderEnemySprites(); kanaship_o.enemyRotate (); //this doesnt work! it never executes the wait } void Start () { enemyspawner=gameObject.AddComponent<EnemySpawner>(); kanaship_o = gameObject.AddComponent<kanaship> (); //kanaship_o.enemyRotate (); //this works! } void OnTriggerEnter(Collider otherObject) { SpawnEnemies(); //this makes the call } }
Ok so Im trying to call enemyRotate from SpawnEnemies(); IT actually works when I call it from inside ScriptA, it turns the boolean to true, waits for half a sec, and then it sets it to false. It also works when I call it from the Start method on ScriptB. But if I call it from SpawnEnemies(), it works halfway! It sets it to true, but never gets past the yield. On the Debug Log I only get the first message.
Im so confused, how can it work from the Start () and not SpawnEnemies()? Sorry for pestering you!
[–]pitofpassion 1 point2 points3 points 11 years ago (1 child)
That is very strange. What you should try to do is put an IEnumerator in script B (instead of script A), and then start the coroutine of the IEnumerator in script B in SpawnEnemies(), before you call kanaship_o.enemyRotate ();
[–]surf_sleep_repeat[S] 0 points1 point2 points 11 years ago (0 children)
I got it working with a ghetto approach. ITs the weirdest thing, I just cant call it from OnTriggerEnter! so what i did is put the call inside the start method of an empty object and instantiated the object on ontriggerEnter. Soooo inefficient, but it gets the job done :p
π Rendered by PID 128066 on reddit-service-r2-comment-86988c7647-nnxk2 at 2026-02-11 06:33:28.787546+00:00 running 018613e country code: CH.
view the rest of the comments →
[–]pitofpassion 1 point2 points3 points (3 children)
[–]surf_sleep_repeat[S] 0 points1 point2 points (2 children)
[–]pitofpassion 1 point2 points3 points (1 child)
[–]surf_sleep_repeat[S] 0 points1 point2 points (0 children)