1
\$\begingroup\$

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):

if(Input.GetButton("Fire1"))
    {
        print("firing");
        GameObject newOrb = (GameObject) Instantiate(MagicOrb, this.transform.position, Quaternion.identity);
        BasicAttackOrb orbControl = newOrb.GetComponent<BasicAttackOrb>();
        if (MoveHandler.facingRight)
            orbControl.SetDirection(BasicAttackOrb.RIGHT);
        else if (!MoveHandler.facingRight)
            orbControl.SetDirection(BasicAttackOrb.LEFT);

    }

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.

\$\endgroup\$
  • \$\begingroup\$ is MagicOrb assigned in the editor inspector? \$\endgroup\$ – dnk drone.vs.drones May 12 '15 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 '15 at 6:57
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

if(MoveHandler.facingRight)
    -> 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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.