- Home /
Cannot set boolean variable from another script
I have code that makes an object rotate, and its working ok if I call it from within the script.
ScriptA
public class ship : MonoBehaviour {
private bool toRotate=false;
public void enemyRotate()
{
toRotate = true;
Debug.Log("er "+ toRotate);
}
void Update () {
if (toRotate)
{
transform.RotateAround(player.transform.position, Vector3.forward, 100 * Time.deltaTime);
}
}
If I call enemyRotate from within scriptA, sure enough I get the rotation, and the debug log shows the variable has been set to true. But if I call it from another function like this:
Script B
public class Projectile : MonoBehaviour {
public ship ship_object;
void Start(){
ship_object=gameObject.AddComponent<kanaship> ();
}
void callFunction(){
ship_object.enemyRotate()
}
}
If I call it from callFunction, I get the Debug log saying the variable has been set to true, BUT the rotation animation is not working at all. I have tried different variants of this but cannot figure out what Im doing wrong. I have even used a public bool and tried to set that to true from another script but nothing. Help?
ship_object
isn't pointing to the first script - that component type is '`ship`'
Answer by AyAMrau · Jun 27, 2014 at 11:37 AM
When using AddComponent
you need to use the Class name for the component. So in your case:
ship_object = gameObject.AddComponent<ship> ();
Your answer
Follow this Question
Related Questions
Rotation help! 1 Answer
How to cover buttons with game objects? 1 Answer
My ship destroyed when i press fire 1 Answer