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

What the best way to go about creating a social class

Discussion in 'Scripting' started by GigiB, Sep 6, 2014.

  1. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Im trying to make a game where one aspect is my citizens (NPCs) will be taxed as a source of income for the city , the way I decided to go about it was to create a function that calculates their gross income and then use this to number to put them into a certain "class" I.e. poor, and middle. I used a Array containing the different classes

    To sum up my question, Whats the best way to go about this, using list, enum, array, or dictionaries? How do you make an if statement based on a number being in a certain range?

    Here's the code I commented out a lot of the unused variables:
    Code (CSharp):
    1.  using UnityEngine;
    2.     using System.Collections;
    3.     using System.Collections.Generic;
    4.    
    5.    
    6.     public class Class_System : MonoBehaviour
    7.     {
    8.    
    9.         //public float income;
    10.         //public string job;
    11.    
    12.      
    13.         string[] finClassArray = {"Poor", "Middle Class", "Elite"};
    14.         public int myHourly;
    15.         private int myMonthly;
    16.         public  int myGross;
    17.    
    18.    
    19.    
    20.         void Start ()
    21.         {
    22.    
    23.             _monthlyIncome ();
    24.             _grossIncome();
    25.    
    26.             if ((Mathf.Clamp (myGross, 0, 4999))  {
    27.                 Debug.Log ("We made it");
    28.             }
    29.    
    30.         }
    31.    
    32.    
    33.         void Update ()
    34.         {
    35.    
    36.         }
    37.    
    38.         public int _monthlyIncome()      //calculate Monthly Income
    39.         {
    40.             myMonthly = myHourly * 31;
    41.             //myGross = myMonthly * 12;
    42.            
    43.             Debug.Log(gameObject.name);
    44.             Debug.Log("Makes");
    45.             Debug.Log (myMonthly);
    46.            
    47.             return myMonthly;
    48.         }
    49.        
    50.         public int _grossIncome()                     //calculates Gross income
    51.         {
    52.             myGross = myMonthly * 12;
    53.            
    54.             Debug.Log (myGross);
    55.            
    56.             return(myGross);
    57.         }
    58.     }
    59.    
    60.  
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I would have a class that manages the classes. ClassManager, then have it so you can pass in a value, in this case the income, and have it return an enum that represents the type of class they are.

    public class ClassManager : System.Object {

    public enum ClassType {
    Poor,
    MiddleClass,
    UpperClass
    }
    public static ClassType GetClassType(int income) {
    // if statement here
    return ClassType;
    }
    }

    Sorry, typed this on mobile. If you do a google search you can find the income ranges on how the government ranks households based on income. Hope this helps.
     
  3. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Yes it does thank you, also (if willing). Can you tell me how to access a variable from another script with out making it static.

    I used the http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
    I get an error saying "cannot explicitly convert.... "


    Code (CSharp):
    1. Class_System class_system = GetComponents<Class_System> ();
    The idea is that I access the "myGross" variable in order to make a function to calculate how much each NPC is taxed

    Code (CSharp):
    1. //Maybe somthing like
    2.  
    3. void Start ()
    4. {
    5. taxation();
    6. }
    7.  
    8. public float taxation()
    9. {
    10. mygross * .07f  ;
    11. }
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Well it depends on how you want to go about things. What will be the purpose? I know you said the taxation but for what reason? Depending on the answer will spark the most optimal way of doing things. (Singleton might be the way to go so far).
     
  5. GigiB

    GigiB

    Joined:
    Sep 5, 2014
    Posts:
    14
    Well the game i'm trying to make is a gonna be like a village management simulator. The taxation will be one of the major sources of income for the village I'm managing along side trade and some other things.

    sorry it took so long to reply, I hope that the answer you where looking for