- Home /
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:
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.
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.
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);
}
}
}
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
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>();
As I wrote above, it can lead to problems when you use the same name for fields/variables and Types.
Your answer
Follow this Question
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