Find var GameObject

Hi there,

I have a little script, just some basics, with a var Transform, and I am trying to get the code to search for the var.

Here is the basic script that work:

unction OnTriggerStay (other: Collider) {

		print ("Triggered");
		
        if (Input.GetKey("g")){
		   	Destroy (gameObject, 0.1);
		   	GameObject.Find("MyObject").SendMessage("HitKey");
			print ("Destroyed");
		}
}

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");
		}
}

Any help perhaps?

Cheers

Jakes

Try

GameObject.Find(CustomObject.gameObject.name).SendMessage("HitKey");

Thanks for the prompt reply :smile:

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.

Is CustomObject assigned to anything? Does the console print “Destroyed”? and make sure to use gameObject.name and not GameObject.name.

Yes, I have assigned a GameObject , yes.
No , the Console does not print “Destroyed”.

I do not know if it can be done, so I am just trying. The basic script work quite fine.

This should go on the object you wanna stand on while pressing G

var CustomObject: Transform;

function OnTriggerStay (other: Collider) 
{
    print ("Triggered");
           
    if (Input.GetKey("g"))
    {
        Destroy (gameObject, 0.1);
        GameObject.Find(CustomObject.gameObject.name).SendMessage("HitKey");
        print("Destroyed");
    }
}

And this should go on the CustomObject

function HitKey () 
{
   // Message Recived!
   // Do your thing..
}

I just tested it and it works fine.

If you already have a reference to the GameObject, why are you calling ‘Find’?

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!

Thanks a million!

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");
    }
}

Nope, you have a reference to the door. So you just call it on the door.

And why does your door have a field ‘door’? And why isn’t it ever set?

if(door.transform.localEulerAngles.y < angleOpen) {
door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
}

Ok, I think I undestand I do’t need to search if I already have the door reference… Thanks

What do you mean by the field door is’t set?