Add values of objects from a layout group?

I want to take a component s value from each children (the number of children can vary) and add them to a total score. I m making a card game called Gwent from the witcher 3. however, I m having trouble getting a value that I have preset to all of the current cards, that I have have, to add up and display a total score.

Here is my declaration code for the card values:

    using UnityEngine;
    using System.Collections;
  
    public class CardValue : MonoBehaviour {
  
        public int cardNumber = null;
  
    }

Basically what I want to have as a finished product is: Take the cardNumber of each card in a layout’s children and add them together and display as a number on the UI. The number of cards in a layout’s children can vary obviously because you’ll play more than one card at a time.

Image Explanation

Here is my code to add up the values:

    using UnityEngine;
    using System.Collections;
  
    public class TableAdder : MonoBehaviour {
  
        public int scoreBoard;
      
        void Start () {
  
        }
  
        void Update () {
          
        }
    }

I know my code is empty. I m not asking help so someone else can code this in for me. I want to expand my knowledge on how to code and figure it out myself. I m just not sure what kind of statement I m supposed to use to get all values from children of a horizontal layout and add them together to display them as one number.

This just feels weird on so many levels.

First off all, please make your forum title be anything other than what it is. Anything would be better.

Secondly, how about not entirely ripping off a copyrighted game? Make your own version. :slight_smile:

Now to your issue. You’ve got no code at all. This tells me you might be new to programming. I promise this is a legitimate answer even though it might seem like I’m trolling or something. Go learn how to program a little before you start making games. Make a console application and have fun. Don’t worry about graphics or physics or anything. Just get something simple working. What you’re wanting to do is super, super simple, but it requires knowing the basics first. In just a few days of dedicated learning you’ll be able to come back to this and accomplish what you need to accomplish. :slight_smile:

Firstly, I didn’t mean to make the title “add” I thought I typed something else in but obviously not.

secondly, I’m not ripping off a game if i’m not trying to make money. Many people would like to see a standalone game so I’m trying to make one. There is a tabletop simulator ver. this game so I’m surely not the first to attempt to make a standalone.

thirdly, yes i’m new to programming, but I want to know what function to use within unity’s program language to gather the values of each card and add them all together. I can figure out the rest on my own. I don’t want to be stabbing around blindly trying to find out a, as you like to call it, “simple” function to execute. I’m currently in my first semester, first year of college with pretty basic classes. (nothing about programming yet) I’m attending as you would have guessed. for programming but I can’t wait to get into it so I want to try and get a head start.

in conclusion i’ll ask nicely once more. Can you just give me a function that is usable in my case so I can move forward with my project? or are you going to just go tell me, for lack of a better phrase, “go learn some sh**”?

edit: would a GetComponentsInChildren work? or is it something else. I know a little bit of the words n’ stuff but I’m not sure which one to use for my particular case.

I’m pretty sure I said the opposite of piss off. This is a common issue with people new to programming. They think they can just jump in and start making whatever they want. It couldn’t be farther from the truth. I’m just some random guy on the internet so I’m sure my words will fall flat. But I’ll say it again…

Learn to program before trying to make a game. Like, seriously. This isn’t a “piss off” statement. I spent about 6 months learning python making command line apps that did things like manipulate strings, do math problems, etc. Then I started poking at while loops to make a “process” to allow for user interaction. I made a couple of command line games and spent a month or so on each, exploring different aspects, running into new and complex issues at every turn.

Fast forward about 4 years, and here I am. Self taught mostly, I’ve got about 5 games under my belt, plus about 10 projects at varying stages. I still run into new complex issues every day and am constantly reminded that I don’t know very much. Sometimes at work on my lunch I’ll whip up a quick console app and explore and idea. Just the other day I wondered how hard it would be to create a basic economy simulator. Spent about 3 hours in total on it and got something neat working, but it was pretty basic.

My point is, don’t be a hasty hobbit. I’m super serious when I say I’m not telling you to piss off. Spend a week learning the basics of C# and I PROMISE you’ll come back to your issue today and go “Oh ok, this is a piece of cake”. You’re gonna loop through your objects, get the sum, assign to the UI, done. Child’s play.

Let me also say this. Assuming you can have a good attitude, which isn’t shown in your response, the community here, myself included, is more than willing to help you learn, but no one will ever do it for you.

Considering your last paragraph there you didn’t read my OP. I said in the last paragraph of my OP “I’m not asking help so someone else can code this in for me”. And if I remember in my first post I wasn’t being unpleasant. So in response to “Assuming you can have a good attitude, which isn’t shown in your response, the community here, myself included, is more than willing to help you learn, but no one will ever do it for you.” I wasn’t being an ass in the first post so why haven’t I received help if being a nice guy and asking nicely gets me help?

yup, that’s the function you’re looking for to pickup all the children in that UI panel

going to echo jt on this though… “I’m making gwent” is blatant copying, and there is indeed a common misconception that “if I don’t make money it’s ok” which isn’t true :eyes:

“I’m making a game like gwent” (iteration of an idea, bringing in your own works and other inspirations) or “I wanted to figure out how they made gwent” (solo learning project, not going to be going out the world) etc. much better approaches. Don’t limit yourself to their design, make better :smile:

You make a valid point. Not gonna argue.

I’m not claiming to have “made” the game. I’m only trying to make a standalone ver. For both educational and personal benefits.

Thank you. This was all I needed to know. Now I can figure it out myself. Much appreciated.

I responded like 30 minutes after your post, lol. That’s a pretty good turnaround. Plus I’m sure most people just ignored the post because the title said “Add”. I am sorry if I came across as an ass. Not sure why you took it that way. I told you exactly what you should do to solve you problem. Don’t get grumpy if you disagree with my advice. And if you had asked me about the getcomponent call I would have answered. :slight_smile: Happy coding!