Search Unity

Beginner Having Issue With Score System

Discussion in 'Scripting' started by farishindi, Oct 3, 2021.

  1. farishindi

    farishindi

    Joined:
    Mar 18, 2021
    Posts:
    2
    Hey, I am VERY new to programming. Ive basically have learned all the basics up until loops. So my games recently, while I learn more advanced basics, have been made using very basic logic.

    I'm making a game where a random Prefab is spawned and is made up of 4 collisions, acting as a connect the dots system. If the player presses them in order the shape will disappear and respawn a new shape. Once certain points are pressed I setActive certain sprites to give the effect of drawing a line. All this works pretty well in my opinion and I'm super surprised I managed to create something like this.

    So I have a script attached to the Prefab and a GameManager script handling the timer for the spawns.

    I'm having trouble now taking the next step forward by implementing logic so if the player completes connecting the dots while it spawned, you get a point. I thought it would be as simple as adding an int variable for player score and playerscore++ if that condition is met. But my brain for some reason can't get it to work.

    I simply want to track my score to then tell the GameManager script to change scenes.

    Not only that but I was hoping those of you who are smarter and more experienced could have a look at my code and potentially guide me in the right direction in terms of completing this game or starting over with a different mindset.


    Here is the code on the Prefab (sorry I couldn't figure out how to post the code the cool way)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ConnectDots_Mechanic : MonoBehaviour
    6. {
    7.     public bool NextisOne = false;
    8.     public bool NextisTwo = false;
    9.     public bool NextisThree = false;
    10.     public bool NextisFour = false;
    11.     public bool NextIsBackToOne = false;
    12.     public bool canScore = false;
    13.     public GameObject[] lines;
    14.     public GameObject[] dots;
    15.     public bool spritesON;
    16.     public bool spritesOFF;
    17.     public int playerScore;
    18.     private GameManager gameManagerScipt;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.  
    23.  
    24.     {
    25.         gameManagerScipt = GetComponent<GameManager>();
    26.  
    27.         spritesOFF = false;
    28.  
    29.         foreach (GameObject _lines in lines)
    30.         {
    31.             _lines.SetActive(false);
    32.         }
    33.         foreach (GameObject _dot in dots)
    34.         {
    35.             _dot.SetActive(true);
    36.         }
    37.  
    38.         NextisOne = true;
    39.     }
    40.  
    41.  
    42.  
    43.     // Update is called once per frame
    44.     void Update()
    45.     {
    46.         if (Input.GetMouseButtonDown(0))
    47.         {
    48.  
    49.             RayCast();
    50.         }
    51.     }
    52.  
    53.  
    54.     public void RayCast()
    55.     {
    56.  
    57.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    58.         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
    59.  
    60.         if (hit)
    61.         {
    62.  
    63.             if (hit.transform.CompareTag("1") && NextisOne)
    64.             {
    65.                 //Debug.Log("1");
    66.                 NextisOne = false;
    67.                 NextisTwo = true;
    68.                 NextisThree = false;
    69.                 NextisFour = false;
    70.                 NextIsBackToOne = false;
    71.                 spritesOFF = false;
    72.                 canScore = false;
    73. }
    74.             else if (hit.transform.CompareTag("2") && NextisTwo)
    75.             {
    76.                 lines[0].SetActive(true);
    77.                // Debug.Log("2");
    78.                 NextisOne = false;
    79.                 NextisTwo = false;
    80.                 NextisThree = true;
    81.                 NextisFour = false;
    82.                 NextIsBackToOne = false;
    83.                 spritesOFF = false;
    84.                 canScore = false;
    85.             }
    86.             else if (hit.transform.CompareTag("3") && NextisThree)
    87.             {
    88.                 lines[1].SetActive(true);
    89.                // Debug.Log("3");
    90.                 NextisOne = false;
    91.                 NextisTwo = false;
    92.                 NextisThree = false;
    93.                 NextisFour = true;
    94.                 NextIsBackToOne = false;
    95.                 spritesOFF = false;
    96.                 canScore = false;
    97.  
    98.             }
    99.             else if (hit.transform.CompareTag("4") && NextisFour)
    100.             {
    101.                 lines[2].SetActive(true);
    102.                // Debug.Log("4");
    103.                 NextisOne = false;
    104.                 NextisTwo = false;
    105.                 NextisThree = false;
    106.                 NextisFour = false;
    107.                 NextIsBackToOne = true;
    108.                 spritesOFF = false;
    109.                 canScore = true;
    110.  
    111.             }
    112.             else if (hit.transform.CompareTag("1") && NextIsBackToOne && canScore)
    113.             {
    114.                 playerScore++;
    115.                 lines[3].SetActive(true);
    116.                 spritesOFF = true;
    117.                 NextisOne = true;
    118.                 NextisTwo = false;
    119.                 NextisThree = false;
    120.                 NextisFour = false;
    121.                 NextIsBackToOne = false;
    122.                 Debug.Log(playerScore);
    123.                 canScore = false;
    124.  
    125.             }
    126.            
    127.         }
    128.  
    129.     }
    130.     public void SpritesOFF()
    131.     {
    132.      
    133.         Destroy(gameObject);
    134.        
    135.     }
    136.  
    137.  
    138. }
    139.  
    140.  


    Here is the GameManager script


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.  
    8.     public GameObject[] shapeList;
    9.     public Transform spawnPosition;
    10.     public float timeBetweenSpawns;
    11.     public float startTimeBetweenSpawns;
    12.     public float decreaseTime;
    13.     public float minTime = 0.65f;
    14.     private int lastRandomFood = 1;
    15.     private int lastSpawnPosition;
    16.     private GameObject currentShape;
    17.     private GameObject currentSpawn;
    18.     private GameObject lastSpawn;
    19.     public ConnectDots_Mechanic Dots_MechanicScript;
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         Dots_MechanicScript = GetComponent<ConnectDots_Mechanic>();
    25.      
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         RandomSpawnLoop();
    32.  
    33.     }
    34.  
    35.     public void RandomSpawnLoop()
    36.     {
    37.         int randomShape = Random.Range(0, shapeList.Length);
    38.         lastSpawn = currentShape;
    39.  
    40.         if (timeBetweenSpawns <= 0 && randomShape != lastRandomFood)
    41.         {
    42.             currentShape = Instantiate(shapeList[randomShape], spawnPosition);
    43.             timeBetweenSpawns = startTimeBetweenSpawns;
    44.             lastRandomFood = randomShape;
    45.  
    46.             if (startTimeBetweenSpawns > minTime)
    47.             {
    48.                 startTimeBetweenSpawns -= decreaseTime;
    49.             }
    50.  
    51.         }
    52.         else
    53.         {
    54.             timeBetweenSpawns -= Time.deltaTime;
    55.         }
    56.  
    57.         Destroy(currentShape, startTimeBetweenSpawns);
    58.  
    59.         Dots_MechanicScript = currentShape.GetComponent<ConnectDots_Mechanic>();
    60.  
    61.         if (Dots_MechanicScript.spritesOFF)
    62.         {
    63.          
    64.             Dots_MechanicScript.SpritesOFF();
    65.             RandomSpawnLoop();
    66.            
    67.         }
    68.     }
    69. }
    70.  
    71.    
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Welcome!

    Unfortunately it is really not possible to generally look at code and offer guidance. What code looks like is far less important than how well it solves your particular problem, or how well it COULD solve your problem.

    To learn more about the logic of connecting dots like you want, I suggest you set this project gently aside and go try some simple puzzle or connect-dots game tutorials on Youtube. Do them in a separate project so you don't mess up yours, and you will learn a lot of the necessary steps.

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

    How to do tutorials properly:

    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 single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. 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 it. Google is your friend here. Do NOT continue until you fix the error. The 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!
     
  3. farishindi

    farishindi

    Joined:
    Mar 18, 2021
    Posts:
    2
    Thank you so much for the guidance. I created this project for my first game jam so it is for sure rushed, so I certainly did not take the steps to learn the basics of building a game like this. You would honestly be surprised about the lack of "connect the dots" game tutorials online. When I search "simple connect the dots game unity tutorial" I just get results for units DOTS and a game made in unity with a similar mechanic. I think searching for something more along the lines of "clicking objects in order".

    I think you are right. I need to just sit back and rethink how this game should work and do as you said. I really like the idea of speaking to my house plant (I do not have a dog sadly). I'm honestly going to take the steps you outlined seriously on my journey. I really appreciate the long reply, I don't really have anyone to go to for help and have never used a forum before. So thanks a bunch! I will for sure be using this forum when I need to.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Oh yeah, that's an unfortunate same-name isn't it!!

    Here are some ultra simple small steps for you to try, and each one of these parts will have LOTS of google-able stuff:

    - make two sprites
    - turn the sprites OFF normally (say, in Start())
    - In update, make something that notes where your finger touches initially and saves it
    - enable the first sprite at that location
    - enable the second sprite and make it follow your finger as long as it is down

    When you finger comes up, turn both sprites OFF again

    When you get that working, add a LineRenderer that has its ends where those sprites are

    - turn the line renderer on when you touch, turn it off when you release.

    Once you have that working, you would have the rudiments of being able to detect clicking on a node to start, and not allowing it to start unless you are ON a node, and not allowing a line to complete unless you are ON another node!

    If you wanna see some of my mouse/touchscreen stuff you're welcome to poke through it in my proximity_buttons project.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/