0
\$\begingroup\$

I need some help with coding this idea: "For each Target destroyed, increase the Gunner's speed by (speed variable)". The idea is that the Gunner is automatically moving, and I want it to become more frantic with each target hit.

Here's the relevant code I have so far:

   using UnityEngine;
   using System.Collections;
   using System.Collections.Generic;

    void Update () {
            Gunner_Move (); 
    }


    //gives me the error "Assets/_Scripts/MovementPathScript.cs(60,83): error CS0200: Property or indexer `System.Array.Length' cannot be assigned to (it is read only)"
    void Gunner_Move(){
            if (GameObject.FindGameObjectsWithTag ("BulletProgressionObject").Length -= 1)
                Gunner_SpeedIncrease ();
    }

void Gunner_SpeedIncrease(){ //behavior that I'm not sure how to make }

    //for the bullets colliding with target's parent object
        void OnTriggerEnter(Collider other){
            if (other.gameObject.CompareTag ("BulletProgressionObject"))
                Destroy (other.gameObject);

I think it's a foreach loop, but the issue I'm having is properly implementing it. Any suggestions or referrals would be greatly appreciated.

\$\endgroup\$
5
  • \$\begingroup\$ The line GameObject.FindGameObjectsWithTag ("BulletProgressionObject").Length -= 1 means "decrease the array length by one" which obviously doesn't make sense because 1. array lengths are read-only, 2. it's a temporary array generated on-the-fly by FindGameObjectsWithTag and 3. it's in an if-condition and assignments in if-conditions are not allowed. What do you really want to do in that line? What's a BulletProgressionObject anyway? \$\endgroup\$
    – Philipp
    Commented May 4, 2016 at 12:18
  • \$\begingroup\$ It's comprised of a Target and Laser Gate. The Bullet can pass through the Laser Gate, but the Gunner can't. BulletProgressionObject is just an arbitrary name I gave the parent game object. \$\endgroup\$ Commented May 4, 2016 at 12:49
  • \$\begingroup\$ OK, and why are you counting how many of these you have in the scene and then try to decrease their count by one? \$\endgroup\$
    – Philipp
    Commented May 4, 2016 at 13:02
  • \$\begingroup\$ You are dodging the question. Is what you are actually trying to do in that line is count how many of these objects you still have in the scene and then set the speed accordingly? In that case I would expect that Gunner_SpeedIncrease is called Gunner_SetSpeed and takes a number as an argument. \$\endgroup\$
    – Philipp
    Commented May 4, 2016 at 13:45
  • \$\begingroup\$ Not dodging, just misunderstood. Anyway, yes that's what I'm trying to do. \$\endgroup\$ Commented May 4, 2016 at 13:58

1 Answer 1

3
\$\begingroup\$

Rather than calling GameObject.FindGameObjectsWithTag every frame, you could just call Gunner_SpeedIncrease() within OnTriggerEnter when the trigger/collision happens.

void OnTriggerEnter(Collider other) {
    if (other.gameObject.CompareTag ("BulletProgressionObject")) {
        Destroy (other.gameObject);
        Gunner_SpeedIncrease ();
    }
}
\$\endgroup\$

You must log in to answer this question.

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