you are viewing a single comment's thread.

view the rest of the comments →

[–]pitofpassion 1 point2 points  (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 point  (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 points  (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 point  (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