Search Unity

Question counting

Discussion in 'Prefabs' started by person90111, Oct 6, 2022.

  1. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
    I have a prefab with a script attached to it, I am trying to access a text GameObject to count up, I tried plenty of things. My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class TreeItem : MonoBehaviour
    7. {
    8.     public GameObject obj;
    9.     public float itema = 0;
    10.     void Start()
    11.     {
    12.         player = GameObject.FindGameObjectWithTag("tree").GetComponent<Transform>();
    13.     }
    14.     public void Wreck()
    15.     {
    16.         Debug.Log("clicked");
    17.         itema = itema + 1;
    18.         Destroy(this.gameObject);
    19.     }
    20.     void Update()
    21.     {
    22.         Tree.text = itema.ToString();
    23.     }
    24. }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    What is the issue exactly?
     
  3. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
    the text object will not count up, also the script is on a prefab
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    The code counting up never runs
     
  5. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    You never call Wreck
     
  7. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
    it destroys the object
     
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Yeah, and it's the only place with counting. So it will never count
     
  9. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
    so... how do I make it count up
     
  10. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Put the count code somewhere which runs, like update (once per frame)
    I dont know what you wanna do
     
  11. person90111

    person90111

    Joined:
    May 18, 2022
    Posts:
    20
    also, one thing the text I want to count up is not a prefab but the object that the script is attached to is a prefab
     
  12. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Okay?
    Whatever you want is doable