- Home /
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;
}
}
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.
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();
}
}
Updated my original answer with some code examples.
Your answer
Follow this Question
Related Questions
Injecting data into Prefabs 1 Answer
Score Multiplier by Time 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