Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Grey_Wolf9 · Jan 27, 2019 at 04:36 PM · destroy objectscoreboard

Increasing the score and destroying the object

So I am making this simple game where the player has to click on all the squares until there are no squares left on the screen. The score board should tally how many squares have been collected. When all the squares have been collected, an end menu appears. This is the score script using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

 public class Score : MonoBehaviour
 {
     public Text scoreText;
     public int score;
    
     
 
     void Start()
     {
         //score = GetComponent<Text>();
     }
 
     void Update()
     {
         scoreText.text = "Score: " + score;
         if(Input.GetMouseButtonDown(0))
         {
             score++;
         }
         if (score >= 6)
         {
             SceneManager.LoadScene("end menu");
         }
     }
 }

And this is the destroy square script:`

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Collect : MonoBehaviour {
 
   GameObject Square;
     
 
         
     void OnMouseDown()
     {
         Destroy(gameObject);
         
         
     }
    
 
 }

` As you can see the score increases just with a mouse click, i only want the score to increase when i collect (and destroy) the square. How can I do this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Rugbug_Redfern · Jan 27, 2019 at 06:12 PM

You need the square script to have a reference to the score script, so it can tell it when to increase the score.

Square script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Collect : MonoBehaviour {
 
     void OnMouseDown() {
         FindObjectOfType<Score>().IncreaseScore(); // Find the Score script and tell it to increase the score
         Destroy(gameObject);
     }
 }

Score script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Score : MonoBehaviour {
 
     public Text scoreText;
     public int score;
 
     void Start() {
         scoreText.text = "Score: " + score; // To make sure the score text is accurate
     }
 
     public void IncreaseScore() {
         score++;
         scoreText.text = "Score: " + score;
         if(score >= 6) { // We only need to check if the score is high enough when it increases, not every frame.
             SceneManager.LoadScene("end menu");
         }
     }
 }

I also cleaned up your code a little. :) hope this helps!

Comment
Add comment · Show 1 · Share
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
avatar image Grey_Wolf9 · Jan 27, 2019 at 06:55 PM 0
Share

Thank you tons :) really useful, thank you for your time and assistance

avatar image
1

Answer by tormentoarmagedoom · Jan 27, 2019 at 04:48 PM

Good day.

Its simple, if you make the square scritp to increase the point right before destroy it.

Just need that all squares acess the score variable of tje score script.

Bye.

Comment
Add comment · Show 1 · Share
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
avatar image Grey_Wolf9 · Jan 27, 2019 at 06:54 PM 0
Share

Thanks i will try this out :)

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

98 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

Related Questions

scoreboard problems 3 Answers

Absolute most basic ranking/scoreboard functionality 1 Answer

I don't know how to make a scoreboard/leaderboard 0 Answers

how to destroy all GameObjects ray went through 1 Answer

Enemy health script using raycast not destroying tagged object 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