Now this one, is based on the abovem but instead of the Object named “MyObject”, I would like to apply the object as a variable, and then have the code to search for it the same way as above.
Here is the code, not working yet
var CustomObject: Transform;
function OnTriggerStay (other: Collider) {
print ("Triggered");
if (Input.GetKey("g")){
Destroy (gameObject, 0.1);
GameObject.Find(CustomObject).SendMessage("HitKey");
print ("Destroyed");
}
}
Although I don’t get an error, it is not sending the message. I have tried (CustomObject.GameObject.name) and (CustomObject.“gameObject.name”) as well, with no luck.
Awesome,Cartman, it is working fine.somehow things were just messed up on my side. I’ve deleted them, and re assign everything, and now it is working as advertised!
Was wondering about this too, but i figured he was setting it at runtime or something.
No problem glad i could help! But as lordofduct said if your just drag/droping the CustomObject in the inspector, there is no need to use Find, you could just use CustomObject.SendMessage.
Well, this way I can use the same script, and use it on multiple objects. Or am I wrong now? I am still learning.
The use of this code is to pick up a key, in order to unlock a door. But all the doors does not have the same key, so every door can have a unique key.
Here is the working code tested on multiple doors:
For the door:
var door: Transform;
var key : boolean = false;
var angleOpen: int;
var angleClose: int;
var speedOpen: int =100;
function OnTriggerStay (other: Collider) {
if (key == true){
if(door.transform.localEulerAngles.y < angleOpen) {
door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
}
}
}
function HitKey (){
print("Message Received");
key = true;
}
For the Key
var DoorObjectToOpen: Transform;
function OnTriggerStay (other: Collider)
{
print ("Triggered");
if (Input.GetKey("g"))
{
Destroy (gameObject, 0.1);
GameObject.Find(DoorObjectToOpen.gameObject.name).SendMessage("HitKey");
print("Destroyed");
}
}