Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by farishindi · Oct 03, 2021 at 12:00 AM · unity 2dscore systemorganization

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();
             
         }
     }
 }
 
     
 
 
 
 
 
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Double tap to reduce score by 2 points 0 Answers

High-score Saving Issue 2 Answers

How to save highscores 1 Answer

I have no Idea why my highscore Script isn't working ... 0 Answers

When I add a score, the scoreText just rapidly displays the scores added to itself 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges