Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by imnickb, Apr 8, 2013.

  1. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    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

    Code (csharp):
    1. #pragma strict
    2.  
    3. var greenSquare : GameObject;
    4. var redSquare : GameObject;
    5. var blueSquare : GameObject;
    6. var orangeSquare : GameObject;
    7. var chosenSquare : GameObject;
    8. var beforeGameWait : float = .75;
    9. var afterSimonsTurnWait : float = .75;
    10.  
    11. function Start ()
    12. {
    13.     ClickHandler.waitingForPlayer = false;
    14.     pickRandomSquare();
    15. }
    16.  
    17. function Update()
    18.  
    19. {
    20.  
    21. }
    22.  
    23. function pickRandomSquare ()
    24. {
    25.     yield WaitForSeconds(beforeGameWait);
    26.  
    27.     var randomSquare : int = Random.Range(1, 4);
    28.     if (randomSquare == 1)
    29.     {
    30.         chosenSquare = greenSquare;
    31.         chosenSquare.renderer.enabled = true;
    32.         chosenSquare.audio.Play();     
    33.     }
    34.     if (randomSquare == 2)
    35.     {
    36.         chosenSquare = redSquare;
    37.         chosenSquare.renderer.enabled = true;
    38.         chosenSquare.audio.Play(); 
    39.     }
    40.     if (randomSquare == 3)
    41.     {
    42.         chosenSquare = blueSquare;
    43.         chosenSquare.renderer.enabled = true;
    44.         chosenSquare.audio.Play();     
    45.     }
    46.     if (randomSquare == 4)
    47.     {
    48.         chosenSquare = orangeSquare;
    49.         chosenSquare.renderer.enabled = true;
    50.         chosenSquare.audio.Play(); 
    51.     }
    52.    
    53.     yield WaitForSeconds(afterSimonsTurnWait);
    54.     chosenSquare.renderer.enabled = false;
    55.     ClickHandler.waitingForPlayer =true;
    56. }
    ClickHandler.js

    Code (csharp):
    1. #pragma strict
    2.  
    3. var colorSquare : GameObject;
    4. static var waitingForPlayer;
    5.  
    6.  
    7. function Start ()
    8. {
    9.  
    10. }
    11.  
    12. function Update ()
    13. {
    14.  
    15. }
    16.  
    17. function OnMouseDown()
    18. {
    19.     if (waitingForPlayer)
    20.     {
    21.         colorSquare.renderer.enabled = true;
    22.         colorSquare.audio.Play();
    23.     }
    24. }
    25.  
    26. function OnMouseUp()
    27. {
    28.     colorSquare.renderer.enabled = false;
    29.     waitingForPlayer =!waitingForPlayer;
    30.     SimonSays.pickRandomSquare();
    31. }
    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!
     
  2. AprendaGames

    AprendaGames

    Joined:
    Jan 4, 2012
    Posts:
    49
    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. ;)

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

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    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:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var colorSquare : GameObject;
    4. static var waitingForPlayer;
    5.  
    6. var simonSaysInstance : SimonSays;
    7.  
    8. function Start()
    9. {
    10.     simonSaysInstance = GetComponent(SimonSays);
    11. }
    12.  
    13. function Update ()
    14. {
    15.  
    16. }
    17.  
    18. function OnMouseDown()
    19. {
    20.     if (waitingForPlayer)
    21.     {
    22.         colorSquare.renderer.enabled = true;
    23.         colorSquare.audio.Play();
    24.     }
    25. }
    26.  
    27. function OnMouseUp()
    28. {
    29.     colorSquare.renderer.enabled = false;
    30.     waitingForPlayer =!waitingForPlayer;
    31.     simonSaysInstance.pickRandomSquare();
    32. }
    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:DoSendMouseEvents(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: http://answers.unity3d.com/questions/39069/an-instance-of-type-x-is-required-to-access-non-st.html

    If anybody could help me understand what I'm missing I'd really appreciate it.
     
  4. miguelvesga

    miguelvesga

    Joined:
    Aug 1, 2012
    Posts:
    88
    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.

    Code (csharp):
    1.  
    2. //...
    3. static function pickRandomSquare()
    4. {
    5. }
    6.  
     
    Last edited: Apr 9, 2013
  5. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    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?
     
  6. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    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!
     
  7. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    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:
    Code (csharp):
    1.  
    2. function Start()
    3. {
    4. myScript = GameObject.Find("MyScriptObject").GetComponent(MyScriptName);
    5. }
    6.  
    More info here: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

    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.
     
  8. imnickb

    imnickb

    Joined:
    May 19, 2011
    Posts:
    31
    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!
     
  9. schpitz

    schpitz

    Joined:
    Feb 13, 2011
    Posts:
    24
    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:DoSendMouseEvents(Int32, Int32)"
     
  10. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Code (csharp):
    1. function pickRandomSquare ()
    2. {
    3.      //lots of code
    4. }
    5.  
    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.