- Home /
GetComponent error
I'm not sure what I'm doing wrong, I tried googling but that wasn't much help. It's probably obvious but I just can't figure out what I'm doing wrong. The last line in the function is the problem. Any help is appreciated :)
Here is the error: error CS0309: The type UnityEngine.Material' must be convertible to
UnityEngine.Component' in order to use it as parameter T' in the generic type or method
UnityEngine.GameObject.GetComponent()
void Thing(string name, int damage, int health, float walkSpeed,GameObject gameObj){
this.name = name;
this.damage = damage;
this.health = health;
this.walkSpeed = walkSpeed;
this.gameObj = gameObj;
color = new Color(Random.Range (0, 255),Random.Range (0, 255),Random.Range (0, 255));
material = gameObj.GetComponent<Material>();
}
Answer by Eric5h5 · Sep 07, 2014 at 07:42 AM
A Material isn't a component and therefore can't be used with GetComponent. It's an object that's used in some components such as MeshRenderer.
Your answer
Follow this Question
Related Questions
error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer
Using getcomponent with an array 2 Answers
get script of another gameobject in c# 1 Answer
Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers
C# GetComponent Issue 2 Answers