I currently am creating a script that allows the player character to attack by shooting orbs at foes.

I have the current code to do that (not final code, for testing purposes):

        GameObject newOrb = (GameObject) Instantiate(MagicOrb, this.transform.position, Quaternion.identity);
        BasicAttackOrb orbControl = newOrb.GetComponent<BasicAttackOrb>();
        if (MoveHandler.facingRight)
        else if (!MoveHandler.facingRight)


So I create the new orb projectile, instantiate it, and then attempt to set the direction.

In the orb script:

public class BasicAttackOrb : MonoBehaviour 
    public const int LEFT = -1;
    public const int RIGHT = 1;

    public float orbSpeed = 15.0f;

    public int direction;

    // Use this for initialization
    void Start () 
        direction = 0;


    // Update is called once per frame
    void Update () 
        if (direction == LEFT)
            this.transform.Translate(-1.0f * orbSpeed * Time.deltaTime, 0.0f, 0.0f);
        else if (direction == RIGHT)
            this.transform.Translate(1.0f * orbSpeed * Time.deltaTime, 0.0f, 0.0f);


    public void SetDirection(int dir)
        direction = dir;

So, if the direction is RIGHT (aka 1) it flies right. If it's LEFT (aka -1) it flies left.

However, the orb direction stays at 0, and is never set. This indicates to me that the initial code above in the Input.GetButton("Fire1") block is incorrect.

Am I accessing the GameObject incorrectly? I feel like it's just a slight coding error with objects but for the life of me I can't find it.

  • \$\begingroup\$ is MagicOrb assigned in the editor inspector? \$\endgroup\$ May 12, 2015 at 6:51
  • 1
    \$\begingroup\$ just put in some debug.log to see if your MoveHandler works correctly: since you have if and else if, if direction isn't modified chances are that something isn't working there \$\endgroup\$
    – Leggy7
    May 12, 2015 at 6:57

2 Answers 2


After some more testing, it appears the problem was the Start() method.

Apparently, for whatever reason, the code was being executed as follows:

orbControl instantiated -> SetDirection() -> Start()

Thus, because of this, direction remained 0.

So, I changed Start() to Awake(), and all's well.

It appears to me after more research that this is the main difference between Start() and Awake(), and that generally one should look to use Awake() for initialization.


Drop in a couple breakpoints and see if either of the MoveHandler condition blocks are actually visited in the following code :

    -> orbControl.SetDirection(BasicAttackOrb.RIGHT);
else if (!MoveHandler.facingRight)
    -> orbControl.SetDirection(BasicAttackOrb.LEFT);

Since those lines are responsible for changing the direction variable, they seem like the likely culprits.

Alternatively change the 'else if' to 'else' to ensure it hits at least one statement.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .