Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by losingisfun · Sep 09, 2016 at 08:45 AM · instantiateprojectileinstantiate prefabprojectilesinstantiated

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image losingisfun · Sep 09, 2016 at 08:49 AM 0
Share

I should also mention, I tested this without the "disableSelf" Invoke. So I know it's not that...

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with instantiation delay that pooling doesn't fix 0 Answers

How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer

Fire Projectile based on Model rotation 1 Answer

Velocity and AddForce Problems 0 Answers

Problem making a Network projectile Instantiate. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges