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 Vanna5556 · Sep 25, 2018 at 05:39 AM · prefabscript.textnewbiescore system

Multiple objects being destroyed, count for them being destroyed doesnt go up past once.

I have a script that's on a prefab, the script finds a text component to attach it to cause prefabs can't remember things like that. sets the "score" to 0 because its a score counter and then updates the text but whenever an object is destroyed with the script on it, it should add 100 points but instead as my poorly typed title says, it only counts up by 100 once, and then never again. the script is attached to the object that is destroyed, but its attached to each object so I don't understand what ive done wrong

Video showing problem here

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class Destroy : MonoBehaviour {
     public Text scoreText;
     private int score;
 
     // Use this for initialization
     void Start () {
         scoreText = GameObject.FindGameObjectWithTag("Score").GetComponent<Text>();
         score = 0;
         scoreText.text = "Score: " + score.ToString ();
     
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void OnMouseDown ()
     {
         Destroy (gameObject);
         score = score + 100;
         scoreText.text = "Score: " + score.ToString ();
     }
 }
 


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
1
Best Answer

Answer by KittenSnipes · Sep 25, 2018 at 08:06 AM

@Vanna5556 The other answer is indeed correct and if you do any of the Unity tutorials that include a score then you would have your answer but for the sake of giving an answer I will give some scripts with comments.

Oh and some simple instructions:

1) Add the first script to an object that will not be destroyed under any circumstances during gameplay

2) Any object that will be destroyed should have the second script on it and a reference to the object holding the first script so when it is destroyed it can add points to our score.

The first script:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine.UI;
  using UnityEngine;
  
  public class ScoreOnDestroy : MonoBehaviour {
      //Lets get our reference to the text object
      public Text scoreText;
      //Our score variable
      private int score;
  
      // Use this for initialization
      void Start () {
          //Make sure our score is 0 when the game starts
          score = 0;
          //Didplay our current score
          scoreText.text = "Score: " + score;
      }
 
      //Function to add points to our current score and display it
      public void AddToScore(int amountToAdd)
          score = score + amountToAdd;
          scoreText.text = "Score: " + score;
      }
  }

The second script:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine.UI;
  using UnityEngine;
  
  public class Destroy : MonoBehaviour {
      //Public variable to get the object with the script holding our score
      public ScoreOnDestroy score;
      //Value the object is worth when it is destroyed
      public int pointsToAdd = 100;
  
      // Use this for initialization
      void Start () {
          //If the score holder was not set then try and grab it from the parent of the script
          if (score == null) {
              score = GetComponent<ScoreOnDestroy>();
          }
      }
     
      void OnMouseDown ()
      {
          score.AddToScore(pointsToAdd);
          Destroy (gameObject);
      }
  }






Comment
Add comment · Show 4 · 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 Vanna5556 · Sep 25, 2018 at 02:01 PM 0
Share

After a small bit of editing I got your instructions to work fine, as long as I manually referenced the object the first script is set on. I don't know what I have to do to reference it automatically because trying to make score or ScoreOnDestroy the game object, it gave an error And i see in the script it references ScoreOnDestroy as a component which is.. impossible because its a seperate game object.

First script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class ScoreOnDestroy : $$anonymous$$onoBehaviour {
     //Lets get our reference to the text object
     public Text scoreText;
     //Our score variable
     private int score;
         
     // Use this for initialization
     void Start () {
         //$$anonymous$$ake sure our score is 0 when the game starts
 
         score = 0;
         scoreText = GameObject.FindGameObjectWithTag("Score").GetComponent<Text>();
         //Display our current score
         scoreText.text = "Score: " + score;
     }
 
     //Function to add points to our current score and display it
     public void AddToScore(int amountToAdd) {
         score = score + amountToAdd;
         scoreText.text = "Score: " + score.ToString();
     }
 }
 


Second Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 public class Destroy : $$anonymous$$onoBehaviour {
     //Public variable to get the object with the script holding our score
     public ScoreOnDestroy score;
     //Value the object is worth when it is destroyed
     public int pointsToAdd = 100;
 
     // Use this for initialization
     void Start () {
         //If the score holder was not set then try and grab it from the parent of the script
         if (score == null) {
             score = GetComponent<ScoreOnDestroy>();
         }
     }
 
     void On$$anonymous$$ouseDown ()
     {
         score.AddToScore(pointsToAdd);
         Destroy (gameObject);
     }
 }
 
avatar image KittenSnipes · Sep 25, 2018 at 05:05 PM 0
Share

@Vanna5556 If you want to find it automatically as you say you can tag it or loop through each object in the scene and set score as the object that contains the first script.

 object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
   foreach (object o in obj)
   {
        GameObject g = (GameObject) o;
        if (g.GetComponent<ScoreOnDestroy>() != null) {
            score = g.GetComponent<ScoreOnDestroy>()
        }
   }
avatar image Vanna5556 KittenSnipes · Sep 25, 2018 at 06:38 PM 0
Share

Thank you so much for your help! It works now, helped me learn alot.

avatar image KittenSnipes Vanna5556 · Sep 25, 2018 at 09:01 PM 0
Share

Glad I could help you out

avatar image
0

Answer by tormentoarmagedoom · Sep 25, 2018 at 07:45 AM

Good day,

You are storing a different points variable in each object, so each object is storing 100 points before beeing destroyed, but you dont have a "general" points counter...

You need to have an external general script that store all the points...

Comment
Add comment · 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

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

127 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

Related Questions

How can I apply a text variable to a prefab 2 Answers

prefabs aren't letting me connect a text public thing to it 1 Answer

How to keep score count for each prefab. 1 Answer

GUIText won't update from prefab 1 Answer

How do I slow down while Im Sliding? 0 Answers


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