Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

If you click all buttons you win the game

Discussion in 'Scripting' started by Kanzaki004, Apr 26, 2022.

  1. Kanzaki004

    Kanzaki004

    Joined:
    Apr 19, 2022
    Posts:
    3
    Hello, I'm new, so I don't know much about scripting. Can someone help me with such a script?
    Script should be working like this:
    There are 3 buttons in a game scene (Spot the difference game so the buttons are transparent).
    Each time a button is clicked, add 1 to the game score.
    If all 3 buttons are clicked, player wins the game and displays a congratulations message.

    Thanks for your help!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Maybe you should share what you have already and what's going wrong with it? People aren't just going to write a script for you.
     
    CorWilson likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    This sounds like the PERFECT scope of game to start with.

    You should ask yourself "Can I?" and proceed, one piece at a time, to learn how to do what you want.

    "Can you put a button in the scene?"
    "Can you put three buttons in the scene?"
    "Can you hook up a script that prints 1, 2 or 3 when you press those buttons?"
    etc

    Imphenzia: How Did I Learn To Make Games:



    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons
     
    Kanzaki004 likes this.
  4. Kanzaki004

    Kanzaki004

    Joined:
    Apr 19, 2022
    Posts:
    3
    I have a button and a WinScript scripts going on.

    The button script is tasked to change the image from the transparent image to the circle image. Once it is clicked, it is supposed to add the points onto the "AddPoints" function in the WinScript.

    Here is the button script:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ChangeImage : MonoBehaviour
    7. {
    8.     public Sprite newButtonImage;
    9.     public Button button;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.      
    20.     }
    21.  
    22.     public void ChangeButtonImage()
    23.     {
    24.         button.image.sprite = newButtonImage;
    25.         gameObject.GetComponent<Button>().interactable = false;
    26.         GameObject.Find("PointsHandler").GetComponent<WinScript>().AddPoints;
    27.     }
    28.  
    29. }
    and here is the WinScript:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WinScript : MonoBehaviour
    6. {
    7.     private int pointsToWin;
    8.     private int currentPoints;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.      pointsToWin = 1;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (currentPoints >= pointsToWin)
    19.         {
    20.             transform.GetChild(0).gameObject.SetActive(true);
    21.         }
    22.     }
    23.  
    24.     public void AddPoints()
    25.     {
    26.         currentPoints++;
    27.     }
    28. }
    The WinScript is attached to the PointsHandler empty option which should display the "Win" Mesh renderer when the point tally is met. The image attached is how it looks like.

    However, I have this error currently with my button script and I am unsure what is the issue:
    Assets\Scripts\ChangeImage.cs(26,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

    Hope this is enough information.
     

    Attached Files:

    Last edited: Apr 26, 2022
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You probably just have a typo. Welcome to coding! See Step #1 my previous post... you clearly did not type things perfectly.

    Specifically, the complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
  6. Kanzaki004

    Kanzaki004

    Joined:
    Apr 19, 2022
    Posts:
    3
    I have updated my post with the code tag. Hope it looks better now.

    The error of my code specifically lies with this line:
    GameObject.Find("PointsHandler").GetComponent<WinScript>().AddPoints;

    I have checked this line of code multiple times and I do not think it is a spelling issue.

    I have gotten this code from an online tutorial regarding another type of game and I tried to apply it to my code. I am not sure if it is applicable for my case. If this does not work, I am looking for advice on what type of scoring system I can apply for my game.

    Thanks.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    This is not how you invoke a method. You invoked two other methods in that same line of code the correct way, so why are you doing it differently here?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You did not get it in that form, I am sure. Reason: I am positive that the invoked the AddPoints function correctly, otherwise the tutorial would be useless to everybody, even the maker.

    I have given you the advice above. Start small. Get a tiny script that prints something or makes a noise when you click a button. There are literally thousands of tutorials. If you find one that doesn't work, move onto the next.

    Nobody on the forums here can PUSH this knowledge into your brain. That's not a thing. You need to make the effort to learn. That's how learning works.