Can't seem to call function on other object with identification.

Alright, so what I’m attempting to do here is to have a setup where the player can interact with certain objects within the world. Basically the idea is, the character clicks on the object, and if the raycast hit returns a certain tag, it runs the function on a script that is on the object that was clicked. The object that is clicked runs a function, which checks to see which gameObject it is, and responds accordingly. Unfortunately this is not working for me:

Here is the script that handles the raycast and calling the function:

#pragma strict

function Start () {

}

function Update () {

if(Input.GetButtonDown("Fire1"))	
	{
	    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	    var hit : RaycastHit;
	    if (Physics.Raycast (ray,hit)) 
	    {       
			if (hit.collider.transform.tag == "actionObject") 
			{
				Debug.Log("we hit the actionObject");
				hit.collider.GetComponent(attributes).Action();
			}
		}
	}

}

This may seem weird now, but the idea is that I will have several different objects, and an if statement for each, and it checks to see if the gameobject the script is attached to is the same as the one in the function. This does not work, however.

#pragma strict
var whatGameObject : GameObject;

function Start () {

}

function Update () {

}

function Action(){
Debug.Log("function was called. check for interaction"); //this returns
	
	if(GameObject == "car")
	{
		Debug.Log("car was interacted with"); //this does not.
	}
	

}

basically my question here is, how do I check what object a script is attached to?

Thanks for your help, its been a while since I’ve done any programming.

If you want to query what you’ve called the gameObject you would use

if (gameObject.name == "car")

Note that gameObject is uncapitalised - GameObject is the class, gameObject is the instance of a game object that your script is attached to. Alternatively, if you have a category of object, you could use tags and use .tag == “car” or .compareTag(“car”)

hope that helps

Also note that using only gameObject == “car” would always return false as you compare a gameObject to a string (just wanted to put emphasis on this!)

Thanks all, I’m not sure how I was that stupid, I’ve done this before, but I just couldn’t remember the exact syntax. Thanks.