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 DemSec · Jun 13, 2016 at 08:22 PM · c#collisionprojectileinstantiate prefab

Proper instantiate projectile with variables (C#)

I have seen many Q&As about this topic, but none of them seem to do what I need. So, I have a universal projectile prefab which can be used by both players and enemies with a small script attached containing a list of variables, like its sender id, damage, speed, size, color, etc. When player or enemy instantiates a projectile, it requires to be instantiated with these values, or else their value will be 0 (or null). Here's the concept:

alt text

I need to keep in mind that at any point, multiple projectiles could collide with an enemy, but having the projectile applying damage directly to health negates the ability to later develop a formula for calculating damage through shields, and armor in the enemy script. I'm unsure if the projectile should pass the damage to enemy script to calculate and apply damage, or if the projectile itself will calculate and apply damage to enemy's health.

alt text

Here's the player script:

 using UnityEngine;
 using System.Collections;
 
 public class Player_Script : MonoBehaviour {
 
     public GameObject Projectile;
     public Transform ProjectileSpawn;
 
     public float projectileDamage;
     public float projectileSpeed;
     public float projectileLifetime;
 
     void Update () {
         if (Input.GetButtonDown ("Fire1")) {
 
             //How do I instantiate with variables applied?
 
             Instantiate(Projectile, ProjectileSpawn.position, ProjectileSpawn.rotation);
         }
     }
 }

Here's the projectile script:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile_Script : MonoBehaviour
 {
     public float damage;
     public float speed;
     public float lifetime;
 
     private Rigidbody rb;
 
     void Start () {
         rb = GetComponent<Rigidbody> ();
         rb.velocity = transform.forward * speed;
         Destroy (gameObject, lifetime);
     }
 
     void OnTriggerEnter (Collider other) {
             if (other.tag == "Obstacle" || other.tag == "Enemy") {
             
                 //How do I apply damage to obstacle or enemy here?
 
                 Destroy (gameObject);
         }
     }
 }
   


P.S.:I would very much appreciate an answer with completed scripts which I could easily plug in and figure out how it works.

4.jpg (61.0 kB)
3.jpg (74.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cherno · Jun 13, 2016 at 09:06 PM

    void Update () {
          if (Input.GetMouseButtonDown ("Fire1")) {
  
              //How do I instantiate with variables applied?
  
              Instantiate(Projectile, ProjectileSpawn.position, ProjectileSpawn.rotation);
              Projectile projectileScript = Projectile.GetComponent<Projectile>();
                     projectileScript.damage = 10f;
                     projectileScript.speed= 200f;
                     projectileScript.lifetime = 3f;
                    projectileScript.origin = gameObject;
          }
      }

As a side note, I recommend changing the variable name for the prefab so there is no confusion between it and the Type Projectile.

   private GameObject origin;

 void OnTriggerEnter (Collider other) {
              if (other.tag == "Obstacle" || other.tag == "Enemy") {
              
                  //How do I apply damage to obstacle or variable here?
                 HealthScript healthScript = other.gameObject.GetComponent<HealthScript>();
                 healthScript.health -= damage;
                 if(healthScript.health <= 0) {
                           Debug.Log(gameObject.name + " has been destroyed by " + origin.name);
                           Destroy (gameObject);
                 }
                  
          }
      }

Comment
Add comment · Show 4 · Share
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 DemSec · Jun 13, 2016 at 09:25 PM 0
Share

What is wrong if I get:

Assets/Scripts/Other Test/Player_Script.cs(19,25): error CS0118: Player_Script.Projectile' is a field' but a `type' was expected

avatar image DemSec DemSec · Jun 13, 2016 at 09:44 PM 0
Share

Never $$anonymous$$d, if my script is named Projectile_Script, then it is:

      Instantiate(Projectile, ProjectileSpawn.position, ProjectileSpawn.rotation);
      Projectile_Script projectileScript = Projectile.GetComponent<Projectile_Script>();
avatar image Cherno DemSec · Jun 13, 2016 at 10:19 PM 0
Share

As I wrote above, it can lead to problems when you use the same name for fields/variables and Types.

avatar image DemSec Cherno · Jun 14, 2016 at 01:15 AM 0
Share

Thanks for your help!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Projectile Ricochet After Collision2d 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to delete this projectile after touching anything?? (C#) 1 Answer

Particle System Play Works Inconsistently 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