Search Unity

help me write code

Discussion in 'Scripting' started by the8ai, Sep 7, 2019.

  1. the8ai

    the8ai

    Joined:
    Sep 7, 2019
    Posts:
    1
    help me create a code in which the number of objects that appear will be counted and this will be displayed on the screen
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What have you tried? What aspect isn't working?
     
    Vryken likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    People will help, but we aren't going to do it for you. Show what you tried and tell us what went wrong.
     
    MadeFromPolygons and Vryken like this.
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What was said above is true; we arent just gonna do it for you. That said, i doubt that from the description you gave anybody could even produce the result you are looking for. Which objects? Spawned through the script doing the counting? The objects spawned from other scripts? Are the spawners known or unknown? And so on and so on.
    There are many ways to interprete what you just said, and many different aspects where you may have problems. So as a rule of thumb, always try to be as clear as possible for what it is you want. And where you had troubles when you tried doing it yourself.
     
  5. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Declare a variable
    public int count;


    When an object appears, increment count by 1
    ManagerScriptOrWhatever.count++;


    In a UI script, declare a text variable
    public TextMeshProUGUI countText;
    (make sure you assign in inspector) (also make sure you're
    using TMPro;


    Display the count in the Update() function
    countText.text = "Objects in scene: " + ManagerScriptOrWhatever.count.ToString();
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Why would you display the count in an update function instead of just updating the text when you increment the count?
     
    MadeFromPolygons and Reedex like this.
  7. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    101
     
    MadeFromPolygons likes this.
  8. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    It's not very taxing to update text every frame. Since this guy is new it is the easiest to get working. I never update text in Update() but that's how I started.
     
  9. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    What?! No, since the guy is new best not to be teaching him bad habbits. There is no point in learning something incorrectly before learning it correctly when the information is already at hand to do it right the first time.

    Just because you learnt to do something incorrectly first, doesnt mean thats a good way to encourage others to do it. There is never an acceptable reason to do that in update when it can be done outside of it, it doesnt matter whether it is taxing or not, its simply not good code and you would fail a code review here in industry for that, same as you would fail an assignment back at university.

    OP: same as others have said no-one is going to do it for you. If you need those services best to hire a developer on freelancer, upwork or unity connect https://connect.unity.com/
     
    SparrowGS likes this.
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Code (csharp):
    1. private int objectsWhichAppeared = 0;
    2. public Text ObjectsText;
    3.  
    4. public void CallWhenObjectAppears()
    5. {
    6.     objectsWhichAppeared++;
    7.     ObjectsText.text = objectsWhichAppeared.ToString();
    8. }