Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to create an object spawner score system

Discussion in 'Scripting' started by edmek55555, Jun 1, 2023.

  1. edmek55555

    edmek55555

    Joined:
    May 22, 2023
    Posts:
    3
    Hey guys. I am working on my first unity project. I have a score system that updates text on trigger enter. However I am working on another level and I think it would be cool to have the score represented by buckets. I am sure would need to add an instantiate line to spawn the object, but I really dont know what the best method would be for providing coordinates so they spawn in an even fashion and dnt overlap. I would also be cool with 2D sprites. Could anyone direct me on how I could start making a visual score system like that instead of just updating text?
     
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    105
    There's no need to worry about trying to manually space them evenly and prevent overlap. Instead, let a (UI) Layout Group do the hard work for you!

     
  3. edmek55555

    edmek55555

    Joined:
    May 22, 2023
    Posts:
    3
    Oh wow. That is a great start. Any idea what a script may look like to have a sprite added each time the score increases? It is pretty staight forward with text but I am having trouble imagining what would need to be updated here to add a sprite each time a trigger is entered
     
  4. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    105
    The most straightforward way would be to instantiate a new child object (of the parent with the Layout Group component) each time the score increases. As new children are added, their Layout Group parent will automatically reorganize them.

    Also worth noting: when you instantiate something, you can optionally specify its parent at the exact same time. Check out all the different overloads of the Instantiate method here.
     
    edmek55555 likes this.