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 ogacexbox · Aug 12, 2019 at 05:56 PM · ui2d-platformerprefabsscore systemvoid

Help with simple issue (hopefully)

I have a score system set up so when player hits collider with the tag i have set it deletes the object and adds +1 to the text in the canvas UI, how ever when the player dies and respawns player / clone of the player no longer references the UI text object

How would i wrote this code to automatically find the UI Text inside the canvas.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController2D controller;
     public Animator animator;
     public Text countText;
     public Vector3 respawnPoint;
 
     private int count;
 
     private void Start()
     {
         count = 0;
         SetCountText();
     }
 
     public float runSpeed = 40f;
 
     float horizontalMove = 0f;
     bool jump = false;
 
     // Update is called once per frame
     void Update()
     {
 
         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
 
         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
 
         if (Input.GetButtonDown("Jump"))
         {
             jump = true;
             animator.SetBool("IsJumping", true);
         }
     }
 
     public void OnLanding()
     {
         animator.SetBool("IsJumping", false);
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Cassette"))
         {
             Destroy(other.gameObject);
             count = count + 1;
             SetCountText();
         }
     }
 
     void SetCountText()
     {
         countText.text = "Cassettes: " + count.ToString();
     }
 
     private void FixedUpdate()
     {
         controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
         jump = false;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Aug 12, 2019 at 06:02 PM

Updated. Here is a simple simpleton class that keeps track of your score. All the player does is call into and update the score. Nothing should be on the player. The singleton just sits in the scene on some empty object.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 // This just goes an an empty game object in the scene - not the player
 public class ScoreManager : MonoBehaviour
 {
     // Singleton
     static ScoreManager instance;
     public static ScoreManager Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = FindObjectOfType<ScoreManager>();
             }
 
             return instance;
         }
     }
 
     [SerializeField] Text counterText;  // score text is on this object or something else, but not the player (because it gets destroyed;
 
     int score;
     public int Score
     {
         get { return score; }
         set
         {
             score = value;
             SetText();
         }
     }
 
 
     public void UpdateScore(int value)
     {
         Score += value;
     }
 
 
     public void ResetScore()
     {
         Score = 0;
     }
 
 
     // Text automatically gets updated when score is changed
     private void SetText()
     {
         counterText.text = Score.ToString();
     }
 }
 


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 // Goes on the player character (but you have your own - this is example)
 public class Player : MonoBehaviour
 {
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Cassette"))
         {
             Destroy(other.gameObject);
 
             // Increment our score
             ScoreManager.Instance.UpdateScore(1);
         }
     }
 }


Hope this gives you an some ideas of dealing with your issue.


Comment
Add comment · Show 3 · 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 ogacexbox · Aug 12, 2019 at 06:19 PM 0
Share

So i moved the score script out of player movement into its own script called "Score" and attached that to the player ins$$anonymous$$d, but ins$$anonymous$$d of me dragging the UI text element into the script because its public, how would i change it so the score script searches for the UI text after respawning?

I have no idea how im doing this.. been following videos here and there trying to build something small so i think i have a lot of spaghetti code.

this is the new score script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : $$anonymous$$onoBehaviour
 {
     public Text countText;
     private int count;
 
     public void Start()
     {
         count = 0;
         SetCountText();
     }
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Cassette"))
         {
             Destroy(other.gameObject);
             count = count + 1;
             SetCountText();
         }
     }
     public void SetCountText()
     {
         countText.text = "Cassettes: " + count.ToString();
     }
 
 
 }
 

And this is the Game$$anonymous$$aster Script i have on a _G$$anonymous$$ object this handles my player respawning

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Cinemachine;
 
 public class Game$$anonymous$$aster : $$anonymous$$onoBehaviour
 {
     public static Game$$anonymous$$aster gm;
     public CinemachineVirtualCamera myCinemachine;
     public Score getScore;
 
 
 
 
     void Start()
     {
         if (gm == null)
         {
             gm = GameObject.FindGameObjectWithTag("G$$anonymous$$").GetComponent<Game$$anonymous$$aster>();
         }
     }
 
     public Transform playerPrefab;
     public Transform spawnPoint;
     public void RespawnPlayer ()
     {
         var newPlayer = Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
         Debug.Log("TODO: Respawn Effects");
 
         myCinemachine.m_Follow = newPlayer;
     }
     public static void $$anonymous$$illPlayer (PlayerScript player)
     {
         Destroy (player.gameObject);
         gm.RespawnPlayer();
 
         
     }
 
 
 }
avatar image Vega4Life ogacexbox · Aug 12, 2019 at 08:46 PM 0
Share

Let me put something together as an example.

avatar image Vega4Life ogacexbox · Aug 12, 2019 at 08:57 PM 0
Share

Updated my original answer with some code examples.

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

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

Injecting data into Prefabs 1 Answer

Score Multiplier by Time 1 Answer

Score increases based on number of collisions at time of kill, not on single kill.,Score increases based on collisions at time of death, not based on single kill. 1 Answer

Changing the UI text value from server to Client side 0 Answers

2D Openworld transport game. How to generate the tracks? 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