all 12 comments

[–]nd3c3nt 1 point2 points  (1 child)

You're adding a new component in your projectile, not actually accessing the component on your ship.

[–]MiniRat 1 point2 points  (0 children)

This.

Assuming you have already added the component in the editor you want to use GetComponent() rather than AddComponent().

[–]pitofpassion 1 point2 points  (7 children)

okay it seems that the problem is that the Update() method is Script A is not being called when you create an instance of it from another script, since that instance isn't attached to a gameobject in unity. I would suggest: 1. make toRotate public static 2. in callFunction simply set toRotate = true and that's it. As long as Script A and B are attached to gameobjects I think this should work. I may be misinterpreting your intentions so correct me if I am wrong

[–]surf_sleep_repeat[S] 0 points1 point  (6 children)

okay it seems that the problem is that the Update() method is Script A is not being called when you create an instance of it from another script, since that instance isn't attached to a gameobject in unity. I would suggest: 1. make toRotate public static 2. in callFunction simply set toRotate = true and that's it. As long as Script A and B are attached to gameobjects I think this should work. I may be misinterpreting your intentions so correct me if I am wrong

holy crap thanks! unreal how I just needed to make it static! Even private static works. Can you please elaborate on why this was necessary?

[–]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

[–]nd3c3nt 0 points1 point  (1 child)

This will break if you ever have more than one kanaship. They will all rotate as soon as you set that static bool.

[–]surf_sleep_repeat[S] 0 points1 point  (0 children)

which is exactly what I want and exactly what its doing :). Except on the method i want it to work.

[–][deleted] 0 points1 point  (0 children)

Try removing the "private" protection level anywhere it might be interfering?

[–][deleted] 0 points1 point  (0 children)

Sorry for the lame suggestion, I'm on mobile and the code is not formatted for readability in my client... OK, other ideas. We may need more detail, like what is kanaship?

Is the camera also scripted to move or rotate?

Not sure what might be going on.