Hi!
I want to call my GameStatus Script and call a function as soon as the character collides with the “point” sphere.
I’m getting this error…
The Pickup script is attached to a simple sphere (Point) with “is Trigger” activated.
Pickup script
function OnTriggerEnter (col : Collider){
if(col.name == "First Person Controller"){
var status : GameStatus = col.GetComponent(GameStatus);
status.AddPoint();
}
}
GameStatus script
var points : int;
function AddPoint(){
points += 10;
print(points);
}
Let’s add two lines.
function OnTriggerEnter (col : Collider){
if(col.name == "First Person Controller"){
var status : GameStatus;
status = col.gameObject.GetComponent(GameStatus);
status.AddPoint();
}
}
What happens ?
This may sound too basic but are you sure gamestatus script is on your “first person controller” object and not somewhere else?
You’re right Speed. I actually wanted to make a script which stores some values and is unique.
I came up with this… I made an empty game object(PlayerPoint) first and attached the GameStatus script. Then I’m calling the functions right of this object like this…
function OnTriggerEnter (col : Collider){
if(col.name == "First Person Controller"){
var go : GameObject = GameObject.Find("PlayerPoint");
go.GetComponent(GameStatus).AddPoint();
}
}
This works fine.
Now i thought I can do it on the iPhone version as well. So I build the same stuff. But I’m getting this
GetComponent returns a Component.
AddPoint is part of a GameStatus.
GameStatus is a Component.
Component is not a GameStatus.
Component does not have AddPoint.
You need to tell Unity that the Component you just got is a GameStatus. You can do this either by explicitly casting:
(GameStatus)(go.GetComponent(GameStatus)).AddPoint();
Or by implicitly casting by placing it in a variable:
var status : GameStatus = go.GetComponent(GameStatus);
status.AddPoint();
It has to do with inheritance, and Base classes not knowing what their possible Subclasses are.
I appreciate all your quick responses.
Problem solved.