Object as a Clickable Button

Hey internet friends, Yep I am going to noob it up again. I have an object that I want to act as a button ( It’s a cube right now). I am trying to make it access a function in a script on another object in the scene. The manner in which I am doing this is of course not working. I have similar code elsewhere and its having a party just fine. :shock:
It’s error is “Object reference no set to an instance of an object”. The function that it calls instantiates an object if that is any help. That same function is also called when I press the 1 key. Thanks in advance

function OnMouseDown ()
{

	Debug.Log("CLicked OK!!!!");
	
	// Acess the laser on the slug
	var someScriptA : SlugScr = GetComponent (SlugScr);
	someScriptA.abilityLaser();
}

I doubt this piece of code contains the error message…?

Either there’s no SlugScr (lolwhat) component attached to whatever this is running on, or abilityLaser is failing its job.

Errors give you a line number and a filename; there’s no reason not to post the exact line and tell us which one it is :stuck_out_tongue:

You’ll find that naming all your functions with CapitalLetters instead of halfCapitals will be enormously beneficial; Unity uses that standard itself and it becomes super easy to tell what’s a function and what’s a variable.

Thanks for your replies guys! I will name my functions as such in the future. I still have butt loads to learn. I shall research my issue later tonight using your input. Thank You! :slight_smile:

ok I have looked at this long and hard and I just do not understand why this doesn’t function. I have my speculations that it has something to do with the function maybe instantiating objects or something of that manner.

the Error that I get is: Null reference exception: Object reference not set to an instance of an object. The abilityLaser Function works as I call it by pressing the 1 key. Names are right and the object to instantiate is all good. Any ideas computer pals?

function abilityLaser()
{
		if (abilityLaserTime > 0.0)
		{
			abilityLaserTime -= Time.deltaTime;
		}
		if (abilityLaserTime <=0.0)
		{
			if (Input.GetButtonDown("ActionA"))
			{
				Instantiate(Laser, transform.position + Vector3(laserX,laserY,laserZ), Quaternion.identity);
				abilityLaserTime = 2.0;
			}
		}
}

My guesses, since you still didn’t give us the line that’s throwing the error :stuck_out_tongue:

  1. abilityLaserTime isn’t initialized before use.
  2. Laser isn’t initialized before use.
  3. No SlugScr component.
  4. I don’t recall the exact error message for trying to access an Input button that doesn’t exist, but maybe ActionA isn’t set properly.

Hey Vicenti thank you very much for helping me with this maddening issue. The line that is throwing the error is as follows:

function OnMouseDown ()
{

//Debug.Log(“CLicked OK!!!”);

// Acess the laser on the slug
var someScriptA : SlugScr = GetComponent (SlugScr); // <---------THIS LINE RIGHT HERE AHHHHHHHHHHHHHHHHHHHHH
someScriptA.BallSack();

//foo = GetComponent(Slug);
//foo.abilityLaser();
}

You seem to be saying that the SlugScr script is attached to a different object to the one with the script you’ve posted. If so, you need to get a reference to the SlugScr object and use GetComponent on it:-

var slugObj: GameObject;  // You can assign this in the inspector.
   ...

var someScriptA : SlugScr = slugObj.GetComponent (SlugScr);
someScriptA.abilityLaser();