Beginner needs help with general practice and score system.
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConnectDots_Mechanic : MonoBehaviour
{
public bool NextisOne = false;
public bool NextisTwo = false;
public bool NextisThree = false;
public bool NextisFour = false;
public bool NextIsBackToOne = false;
public bool canScore = false;
public GameObject[] lines;
public GameObject[] dots;
public bool spritesON;
public bool spritesOFF;
public int playerScore;
private GameManager gameManagerScipt;
// Start is called before the first frame update
void Start()
{
gameManagerScipt = GetComponent<GameManager>();
spritesOFF = false;
foreach (GameObject _lines in lines)
{
_lines.SetActive(false);
}
foreach (GameObject _dot in dots)
{
_dot.SetActive(true);
}
NextisOne = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RayCast();
}
}
public void RayCast()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
if (hit)
{
if (hit.transform.CompareTag("1") && NextisOne)
{
//Debug.Log("1");
NextisOne = false;
NextisTwo = true;
NextisThree = false;
NextisFour = false;
NextIsBackToOne = false;
spritesOFF = false;
canScore = false;
}
else if (hit.transform.CompareTag("2") && NextisTwo)
{
lines[0].SetActive(true);
// Debug.Log("2");
NextisOne = false;
NextisTwo = false;
NextisThree = true;
NextisFour = false;
NextIsBackToOne = false;
spritesOFF = false;
canScore = false;
}
else if (hit.transform.CompareTag("3") && NextisThree)
{
lines[1].SetActive(true);
// Debug.Log("3");
NextisOne = false;
NextisTwo = false;
NextisThree = false;
NextisFour = true;
NextIsBackToOne = false;
spritesOFF = false;
canScore = false;
}
else if (hit.transform.CompareTag("4") && NextisFour)
{
lines[2].SetActive(true);
// Debug.Log("4");
NextisOne = false;
NextisTwo = false;
NextisThree = false;
NextisFour = false;
NextIsBackToOne = true;
spritesOFF = false;
canScore = true;
}
else if (hit.transform.CompareTag("1") && NextIsBackToOne && canScore)
{
playerScore++;
lines[3].SetActive(true);
spritesOFF = true;
NextisOne = true;
NextisTwo = false;
NextisThree = false;
NextisFour = false;
NextIsBackToOne = false;
Debug.Log(playerScore);
canScore = false;
}
}
}
public void SpritesOFF()
{
Destroy(gameObject);
}
}
Here is my code for the GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject[] shapeList;
public Transform spawnPosition;
public float timeBetweenSpawns;
public float startTimeBetweenSpawns;
public float decreaseTime;
public float minTime = 0.65f;
private int lastRandomFood = 1;
private int lastSpawnPosition;
private GameObject currentShape;
private GameObject currentSpawn;
private GameObject lastSpawn;
public ConnectDots_Mechanic Dots_MechanicScript;
void Start()
{
Dots_MechanicScript = GetComponent<ConnectDots_Mechanic>();
}
// Update is called once per frame
void Update()
{
RandomSpawnLoop();
}
public void RandomSpawnLoop()
{
int randomShape = Random.Range(0, shapeList.Length);
lastSpawn = currentShape;
if (timeBetweenSpawns <= 0 && randomShape != lastRandomFood)
{
currentShape = Instantiate(shapeList[randomShape], spawnPosition);
timeBetweenSpawns = startTimeBetweenSpawns;
lastRandomFood = randomShape;
if (startTimeBetweenSpawns > minTime)
{
startTimeBetweenSpawns -= decreaseTime;
}
}
else
{
timeBetweenSpawns -= Time.deltaTime;
}
Destroy(currentShape, startTimeBetweenSpawns);
Dots_MechanicScript = currentShape.GetComponent<ConnectDots_Mechanic>();
if (Dots_MechanicScript.spritesOFF)
{
Dots_MechanicScript.SpritesOFF();
RandomSpawnLoop();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613043733im_/https://answers.unity.com/themes/thub/images/avi.jpg)