Making a Simon game and I could use a little help because I'm stuck!

I wanted to try to make a Simon type game as a way to learn some more about Unity. I’m pretty much a beginner when it comes to scripting so I thought this might be a fun way to learn a little more. I started this little project today and everything was moving along swimmingly but now I’ve hit a snag. I wanted to call SimonSays.pickRandomSquare() from the end of the OnMouseUp() function in ClickHandler. When I do so, I get an error about needing an instance of SimonSays to access nonstatic member pickRandomSquare(). If I make that function static, then all the variables in it need to be static. If I make them static, then I can’t assign game objects in the inspector… I’m stuck! How do I create an instance of SimonSays? I thought I was doing that by attaching it to the main SimonSays game object I’m using? I’m trying to learn so any advice would be helpful.

Here are my scripts so far:

SimonSays.js

#pragma strict

var greenSquare : GameObject;
var redSquare : GameObject;
var blueSquare : GameObject;
var orangeSquare : GameObject;
var chosenSquare : GameObject;
var beforeGameWait : float = .75;
var afterSimonsTurnWait : float = .75;

function Start ()
{
	ClickHandler.waitingForPlayer = false;
	pickRandomSquare();
}

function Update()

{

}

function pickRandomSquare ()
{
	yield WaitForSeconds(beforeGameWait);

	var randomSquare : int = Random.Range(1, 4);
	if (randomSquare == 1)
	{
		chosenSquare = greenSquare;
		chosenSquare.renderer.enabled = true;
		chosenSquare.audio.Play();		
	}
	if (randomSquare == 2)
	{
		chosenSquare = redSquare;
		chosenSquare.renderer.enabled = true;
		chosenSquare.audio.Play();	
	}
	if (randomSquare == 3)
	{
		chosenSquare = blueSquare;
		chosenSquare.renderer.enabled = true;
		chosenSquare.audio.Play();		
	}
	if (randomSquare == 4)
	{
		chosenSquare = orangeSquare;
		chosenSquare.renderer.enabled = true;
		chosenSquare.audio.Play();	
	}
	
	yield WaitForSeconds(afterSimonsTurnWait);
	chosenSquare.renderer.enabled = false;
	ClickHandler.waitingForPlayer =true;
}

ClickHandler.js

#pragma strict

var colorSquare : GameObject;
static var waitingForPlayer;


function Start ()
{

}

function Update ()
{

}

function OnMouseDown()
{
	if (waitingForPlayer)
	{
		colorSquare.renderer.enabled = true;
		colorSquare.audio.Play();
	}
}

function OnMouseUp()
{
	colorSquare.renderer.enabled = false;
	waitingForPlayer =!waitingForPlayer;
	SimonSays.pickRandomSquare();
}

Once I get this figured out I plan on creating arrays and iterating through them and checking the Simon Array agains the Player Array to see if the sequence is correct or not. That’s the plan!

You just need to define the SimonSays script in your ClickHandler script. I do c# so sorry if this is not exactly right, but you get the idea. :wink:

var simonScript:SimonSays;
 
function Start(){
simonScript  = gameObject.GetComponent(SimonSays);
}

Thanks so much for the input! I found another thread about this topic but the link is broken for references, so I’m still not really learning what’s going on here. I went ahead and added this to the ClickHandler script:

#pragma strict

var colorSquare : GameObject;
static var waitingForPlayer;
 
var simonSaysInstance : SimonSays; 
 
function Start()
{
	simonSaysInstance = GetComponent(SimonSays);
} 

function Update ()
{

}

function OnMouseDown()
{
	if (waitingForPlayer)
	{
		colorSquare.renderer.enabled = true;
		colorSquare.audio.Play();
	}
}

function OnMouseUp()
{
	colorSquare.renderer.enabled = false;
	waitingForPlayer =!waitingForPlayer;
	simonSaysInstance.pickRandomSquare();
}

I’m still getting this error:

NullReferenceException: Object reference not set to an instance of an object
ClickHandler.OnMouseUp () (at Assets/Scripts/ClickHandler.js:31)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

I see the variable for the script instance in the inspector but I’m not able to drag the script or a game object onto that variable in the inspector. Assigning the script to it in the start function isn’t doing anything either, I just see the variable saying (None) next to it. I’m sure I’m misunderstanding some REALLY basic stuff here, but I can’t find any good resources to help me figure out what I need to do. This post is pretty helpful, but the reference he links doesn’t work anymore: An instance of type "x" is required to access non-static variable "y" - Questions & Answers - Unity Discussions

If anybody could help me understand what I’m missing I’d really appreciate it.

Although i’m not a JS programmer, I think you should add the modifier ‘static’ to the function.
Why? You’re calling a non-static function. To do this you have to create a new instance, but you havent. Therefore, this error: Object reference not set to an instance of an object.

//...
static function pickRandomSquare()
{
}

You don’t have to make the function static. It just can’t find your SimonSays script. Did you attach the script on the same object as your ClickHandler script?

No, I have 4 clickable buttons that have the click handler on them and one background game object with the SimonSays script on it. I thought I’d be able to access the pickRandom function in the SimonSays script from the ClickHandler by calling it like this: SimonSays.pickRandomSquare() but that’s when I start getting the errors about static/nonstatic. I’ve tried making the function static since that’s what the error complains about but then all of the variables inside it need to be static as well and that causes more errors. I’m rather inexperienced with this and really want to learn more. I’m incredibly grateful for all of the insight so far! Thanks!

If your script is not in the same game object as your click handler, you can’t just call GetComponent(SimonSays) because what you’re doing is that you’re telling to script to find the script, SimonSays, in the game object it is currently attached to. What I recommend (for the lazy people), is to do a GameObject.Find to find the game object with the SimonSays script based on the game object name then do a GetComponent.

For example:

function Start()
{
myScript = GameObject.Find("MyScriptObject").GetComponent(MyScriptName);
}

More info here: Unity - Scripting API: GameObject.Find

Another way is to create a public variable (JS makes your variable public by default I believe) and just dragging your object with the script and dropping it into the empty slot in ClickHandler script in the inspector.

Ok, with your help I’ve figured out what’s going on here. I was able to get it to find the script by using the GetComponent function. My problem is I was trying to do it by making a public variable and dragging the game object with the script onto it. In theory this should work, but for some reason Unity wasn’t highlighting it in blue when I tried to drag the game object onto the public variable I made (var simonSaysInstance : SimonSays;). In an act of desperation I just kept trying to drag and drop it a bunch of times like somehow I wasn’t dragging and dropping it right… It turns out if I drop closer to the variable above it in the inspector (which is a String!) it magically gets put into the right variable slot underneath for my script instance. It seems like a bug or something, I can’t believe that’s what’s been the problem!

Would you be able to share this project with us?
I believe you also tried to make it according to the video tutorial (that was made with c#), and i’m very curious about you JS code.
I tried to use the scripts parts from here, but it gives me error:
“NullReferenceException: Object reference not set to an instance of an object
ClickHandler.OnMouseUp () (at Assets/Scipts/ClickHandler.js:37)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)”

function pickRandomSquare ()
{
     //lots of code
}

You can write this a lot simpeler if you use a Switch. Also, if you have code that is repeated a lot, you can best make a seperate function for it that you can call.