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 Dqimass · May 29, 2016 at 01:39 AM · uitextscorerestartadding

Need help with adding score from 2 different script C#

Hi guys! so my first script is when the player pick up an gameobject A, player get 3 points and if the player pick up gameobject B, the player get 5 points.

So as the player keep picking up these objects, the score will keep adding up and this is working fine.

However, I have another script that have gameobject C ( Rocks ) which player have to avoid. if gameobject C collides with a gameobject D (lava), the player get 1 points. (originally, I want the player to get 1 point for avoiding rock, but I find that really difficult to do so i decided to destroy rock and get point instead)

The only problem I am having is that the score does not add both script together as one instead it adds as 2 different score. so if my player pick up 2 gameobject A, the player get 6 points. and if the gameobject C collides with gameobject D, player get 1 points. so the point should have add up to 7 but it didn't, the score went back to 1.

here's the script for the pick up part

 public Text scoreText;
 public int lettuceValue;
 public int wormValue;
 private int point;


 // Use this for initialization
 void Start ()
 {
     point = 0;
     SetCountText ();
 }
 
 // Update is called once per frame
 void Update () 
 {
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("PickUp")) {

         Destroy (other.gameObject);
         point += wormValue;
         SetCountText ();
     } 
     else if (other.gameObject.CompareTag ("PickUp2")) {
         Destroy (other.gameObject);
         point += lettuceValue;
         SetCountText ();
     }
 }

 void SetCountText ()
 {
     scoreText.text = "score:" + point.ToString();
 }

}

and this is the avoid script

 public Text scoreText;
 private int point;


 void Start ()
 {
     point = 0;
     SetCountText ();
 }

 void Update ()
 {
     

 }
 // in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Load"))
     {

         Destroy (other.gameObject);
         point = point + 1;
         SetCountText ();
     }
 }

 void SetCountText ()
 {
     scoreText.text = "score: " + point.ToString ();
 }

}

So how do I get those 2 script scores add everything up as one?

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

4 Replies

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

Answer by Maurdekye · May 29, 2016 at 02:28 AM

Create a third gameobject and attach this script to it;

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PointsManager : MonoBehaviour {
 
     public Text scoreText;
 
     private int Points;
 
     public void Start()
     {
         Points = 0;
         scoreText.text = "score:" + Points;
     }
 
     public void AddPoints(int amount)
     {
         Points = Points + amount;
         scoreText.text = "score:" + Points;
     }
 }

Then, modify your first script to look like this;

 public int lettuceValue;
 public int wormValue;
 public PointsManager manager;
 
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("PickUp"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(wormValue);
     }
     else if (other.gameObject.CompareTag("PickUp2"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(lettuceValue);
     }
 }
 

And your second script to look like this;

 public PointsManager manager;
 // in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Load"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(1);
     }
 }

Then just pass the first empty gameobject as an argument to both of your other scripts. The point total should keep a rolling count from both of them.

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 Dqimass · May 29, 2016 at 02:52 AM 0
Share

Phew it worked xD!!! Thank you so much for taking the time to answer this! $$anonymous$$uch appreciated!

avatar image
-1

Answer by Gaming-Dudester · May 29, 2016 at 02:25 AM

Sorry if I misunderstand BUT wouldn't you do this? put this in avoid script

public PickUpScript pickupscript;

and put this in pick up script

public AvoidScript avoidscript;

and say

      if(//player picks up thingy) 
      {
          avoidscript.scoreint = avoidscript.scoreint + 1;
      }








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
avatar image
0

Answer by naphid · May 29, 2016 at 03:07 AM

Make your points public static instead of private. When they are private it means there can be multiple instances of them.

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
avatar image
0

Answer by Maurdekye · May 29, 2016 at 03:07 AM

Create a third gameobject and attach this script to it;

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PointsManager : MonoBehaviour {
 
     public Text scoreText;
 
     private int Points;
 
     public void Start()
     {
         Points = 0;
         scoreText.text = "score:" + Points;
     }
 
     public void AddPoints(int amount)
     {
         Points = Points + amount;
         scoreText.text = "score:" + Points;
     }
 }

Then, modify your first script to look like this;

 public int lettuceValue;
 public int wormValue;
 public PointsManager manager;
 
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("PickUp"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(wormValue);
     }
     else if (other.gameObject.CompareTag("PickUp2"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(lettuceValue);
     }
 }
 

And your second script to look like this;

 public PointsManager manager;
 // in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("Load"))
     {
         Destroy(other.gameObject);
         manager.AddPoints(1);
     }
 }

Then just pass the first empty gameobject as an argument to both of your scripts.

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

64 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

Related Questions

Which type of Variable do I need to use to change the Value of a text UI object ? 0 Answers

Can't drag text object into inspector? 0 Answers

Score text wont appear 1 Answer

How can i link my code together so the score value affects the timer value 1 Answer

Graphic problem with UI Text 2 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