Well… i’m study some videos and i’m with a little problem. seems to be easy, but i realy dont know how to solve.
I tryed to see “Component” class in the documentation, but nothing.
Here a example:
First file
public class Script_Enemy: MonoBehaviour {
public int ClicksOn = 0;
}
- the second one
public class Script_Refresh: MonoBehaviour {
void Update () {
Component enemyScript;
enemyScript = rhHit.transform.GetComponent<Script_Enemy>();
enemyScript.ClicksOn -= 1;
}
}
Where i’m miss?
Unity sad “error CS1061: Type UnityEngine.Component' does not contain a definition for ClicksOn’ and no extension method ClicksOn' of type UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)”
I’m following this tutorial: http://walkerboystudio.com/html/unity_course_lab_1.html
But when him try to interact with other script of an object… well… javascript only type the variable after name it. C# do that before naming the variable, and then… GetComponent returns a Component.
public class Script_Refresh: MonoBehaviour {
void Update () {
Script_Enemy enemyScript;
enemyScript = (Script_Enemy)rhHit.transform.GetComponent<Script_Enemy>();
enemyScript.ClicksOn -= 1;
}
}
“Component” doesn’t contain the variable “ClicksOn”. You have to tell the compiler that you’re using a kind of component which does have that variable: Script_Enemy.
Imagine this analogy.
class Person
{
name
age
}
class Employee : Person
{
wage
}
All people have a name and an age.
All employees are people.
All employees have a wage.
However, a person doesn’t necessarily have a wage.
If you want to access a person’s wage, he has to be recognized as an employee. However, if all you (and the compiler) knows is that he’s a person, he might not have a wage, and trying to access a nonexistent wage might cause problems.
Well… when i try to do that, i thought i should add a using in top of my file, but only typing the variable as my other script class, it work!
Thanks
The point is not the class inheritance. But like you sad “he has to be recognized as an employee”, MarkusB post’s make me see, what are you telling me. I just type it as my class name.