Hey I’ve tried a lot of different ideas for this but it has me stumped. It’s so simple I’m sure.
I want to have a projectile object detect a collision with an enemy object and reduce a variable named ‘health’ on the enemy object. I want this done from the projectiles game object, obviously.
I have two objects each with the respective scripts attached.
-EnemyParent-
using UnityEngine;
using System.Collections;
public class enemyHealth : MonoBehaviour {
public float health;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (health<=0){
Destroy (gameObject);
}
}
}
ProjBullet
using UnityEngine;
using System.Collections;
public class projBullet : MonoBehaviour {
float lifespan = 3.0f;
public float damage = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
lifespan -= Time.deltaTime;
if(lifespan <= 0) {
Timeout();
}
}
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "Enemy") {
collision.gameObject.GetComponent("enemyHealth").health -= damage;
}
}
void Timeout(){
Destroy (gameObject);
}
}
I’m getting this compile error.
Assets/Projectiles/projBullet.cs(27,74): error CS1061: Type `UnityEngine.Component' does not contain a definition for `health' and no extension method `health' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Clearly my usuage of GetComponent() isn’t right. I’ve tried creating a GameObject variable from the collision.gameObject return and then using GetComponent() on that and I get the same problem.
I thought by using GetComponent() you could access a variable on a specific instance. To me it looks like it’s finding the component I want but not the variable even though I’ve made it public.
Any help and explaination would be greatly appreciated.
A Cast is when you have a object in “disguise” eg. the following code is valid
object o = some_transform;
Now since object does not have the same functionality as a transform all the transform functionality i lost. how ever you can simply cast the transform to a transform object to get this functionality back like this;
Transform t = (Transform)o;
The above code show an example of a explicit cast (If I’m Correct) where using either “is” or “as” is a implicit cast. The above show you the power and base idea of inheritance and polymorphism. For better understanding I would recommend you to read some basic background knowledge about both things since those 2 are widely used within object oriented programming. If you search for either one on Google you should get loads of examples and such about. If you’d like read this: http://www.codeproject.com/Answers/394018/encapsulation-and-abstraction#answer2 should explain the basics and lead you to other sources.
Generic is a world for it self, it work by you’re able to parse types as parameters. I’ve never played much with generic types my self and made my own generic type so I can’t really go much more in depth with it other then saying; Generics makes it possible to use a type as parameter. There is loads about generics on Google too. A good place to start may be here: http://msdn.microsoft.com/en-us/library/sz6zd40f(v=vs.80).aspx
GetComponent returns a component. Unity doesn’t know if it’s a Light, a Transform, an EnemyHealth or something else.
So you can’t make it brighter (since it may not be a light) and you can’t set health (since it may not be an EnemyHealth).
It’s like someone asking you to use his vehicle, you can’t just say yes because you don’t know if it’s a bicycle or a plane
Casting specifies the type of something.
When you get your EnemyHealth via GetComponent, it’s a component, but you as the programmer know that it’s not just a component, but also an EnemyHealth. So you tell that to the program by casting it to an EnemyHealth. Casting makes your program (or at least parts) crash if you ‘lie’ to the code, like Casting a Light to a Transform or something.
It’s like that someone says “don’t worry, it’s just my tricycle” and puts you in an airbus cockpit. You’d kick them just like Unity kicks you.
In JavaScript, Casting works with variable as Type
Generics are Things that work with a type. GetComponent has a generic version that takes a Type instead of its name as a String. The generic version of GetComponent is designed to return an Object of the Type given that way instead of just a component and Unity knows that.
So if you say
GetComponent.<EnemyHealth>()
(notice the dot before the < that’s [only] neccessary in JS)
instead of
GetComponent(EnemyHealth)
Unity knows that this method will return an EnemyHealth (or null if none exists) instead of a not exactly specified Component.
Generics are faster and more awesome than Casting, so you should use it.
Unluckily, I’m lacking a metaphor for it.