- Home /
Projectiles trigger affecting other projectiles of same prefab - C#
This is a bit weird, I have a projectile (Arrow) that when it hits, it's velocity becomes static, and it's collider turns off, the result being an arrow stuck on the ground.
However, when an arrow hits my player, it disappears instead, and damages the player.
After the arrow hits the player, another arrow, sometimes one or two arrows after the first, will also act like it just hit the player. The player doesn't get damaged, but the arrow still acts like it hit, and it disappears in mid-air. It's usually after a slight delay.
What i'm confused about is how instantiated gameObjects, when I change a variable in it's script, during runtime, it will also have changed the prefabs variable, so all instantiated objects will inherit that variable. I've managed to avoid this being a problem for the most part, by setting the projectile stats as its instantiated. The reason I want to keep this feature is so I can dummy the same script and projectile onto a new mob, that can have the same projectile but have it's own stats, damage, force, range, etc.
Is there a way to set a public variable in an instantiated game object (prefab) without affecting anymore potential instantiated game objects?
code -
Projectile:
public class projectileStats : MonoBehaviour {
public bool facingRight;
public int minDamage;
public int maxDamage;
public string damageType;
public int pushForce;
public bool moving = true;
void FixedUpdate () {
if (moving) {
Vector2 dir = transform.GetComponent<Rigidbody2D> ().velocity;
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
}
Projectile collider:
public class projectileCollider : MonoBehaviour {
void FixedUpdate() {
}
void OnTriggerEnter2D (Collider2D other) {
Invoke ("disableSelf", 4.5f);
GetComponentInParent<projectileStats> ().moving = false;
GetComponentInParent<Rigidbody2D> ().gravityScale = 0f;
GetComponentInParent<Rigidbody2D> ().velocity = new Vector2 (0f, 0f);
GetComponent<BoxCollider2D> ().enabled = false;
if (other.gameObject.layer == LayerMask.NameToLayer ("Player") || other.gameObject.layer == LayerMask.NameToLayer ("Allies")) {
this.transform.parent.gameObject.GetComponent<SpriteRenderer> ().color = new Color (1F, 1F, 1F, 0F);
}
}
void disableSelf() {
this.transform.parent.gameObject.SetActive (false);
}
}
Mob projectile instantiation:
void attack () {
projectile = Instantiate (projectile, transform.position, Quaternion.identity) as GameObject;
projectile.SetActive (true);
projectile.GetComponent<projectileStats>().moving = true;
projectile.GetComponent<projectileStats>().facingRight = facingRight;
projectile.GetComponent<projectileStats>().minDamage = minDamage;
projectile.GetComponent<projectileStats>().maxDamage = maxDamage;
projectile.GetComponent<projectileStats>().damageType = damageType;
projectile.GetComponent<projectileStats>().pushForce = pushForce;
projectile.GetComponent<SpriteRenderer>().color = new Color (1F, 1F, 1F, 1F);
projectile.GetComponentInParent<Rigidbody2D> ().gravityScale = projectileGravityScale;
projectile.GetComponentInChildren<BoxCollider2D> ().enabled = true;
projectile.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (shootDirection*(Random.Range (projectileXforce1, projectileXforce2)), Random.Range (projectileYforce1, projectileYforce2)));
}
(Oh yeah, I would set the Alpha colour to '0F', because when i did SetActive(false); it was disabling itself in mid-air, so i thought changing the colour might not affect it, but i was wrong. so meh)
I should also mention, I tested this without the "disableSelf" Invoke. So I know it's not that...