Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 06, 2015 at 08:45 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by the_big_jablonski · Jan 06, 2015 at 12:28 PM · c#enumconstructor

Why is the value of my enum being reset outside of my class constructor?

Hey guys,

I'm attempting to make a class that I can use to create NPC's, specifically football players. It's important to me that each player has different starting attributes (like strength, speed, etc...) relative to their position. For example, an Offensive Lineman needs to be stronger than a Wide Receiver.

In order to do that, I created an enum with basic positions and then used a switch to allocate points to different attributes based on that position. Because I'm not trying to make these NPC's Game Objects but rather only store the values of their attributes and other basic info, the class doesn't inherit MonoBehaviour. Therefor I am using a constructor for my class which is passed two parameters, the player's position, and his star ranking (5 star recruits ought to be better than 3 star recruits).

The problem I seem to be running into right now, and I have no errors mind you, is that the int passed into my constructor and assigned to the enum position seems to reset outside of the constructor (in the methods of the class) to its default. I figured this is more or less what's happening because my Debug.Log(position) in the constructor returns the correct value (that is, the one passed into the constructor) while the two in my methods return the default. :(

What's the deal with constructors? Does their scope not reach through the entire class? Can anyone solve this issue for me? For clarity the position variable is in Line 28, the Debug.Log(position)s are Line 31, Line 70, and Line 96.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NPCconstructor
 {
 
     //PositionStats doesn't have specific positions like RG or CB b/c it's just for making the stats
     //QB == 0 - DB == 7, 8 total
     public enum PositionEnum {QB, RB, WR, TE, OL, DL, LB, DB};
     public PositionEnum position;
     public int stars;
 
     //these floats will store the actual attribute values
     public float strength, acceleration, speed, agility, endurance;
     public float points;
     public string firstName, lastName, fullName;
 
     //these are for the name databases
     public List<string> firstNames = new List<string> ();
     public List<string> lastNames = new List<string> ();
 
     public NPCconstructor(int pos, int sta)
     {
         //assign the arguments to local class variables
         //I'm going to use these to constrict the type of player generated via stats
         PositionEnum position = (PositionEnum) pos;
         stars = sta;
 
         Debug.Log (position);
 
         strength = 65f;
         acceleration = 65f;
         speed = 65f;
         agility = 65f;
         endurance = 65f;
 
         //
         //import txt file of all our English first names
         TextAsset firstNamesENG = Resources.Load<TextAsset> ("FirstNamesENG");
         TextAsset lastNamesENG = Resources.Load<TextAsset> ("LastNamesENG");
         
         //splits the first and last name lists by line 
         string[] linesFirst = firstNamesENG.text.Split ("\n" [0]);
         string[] linesLast = lastNamesENG.text.Split ("\n" [0]);
         
         
         //AND finally adds those array elements into our List
         for (int i = 0; i < linesFirst.Length; i++)
         {
             firstNames.Add(linesFirst[i]);
         }
         for (int i = 0; i < linesLast.Length; i++)
         {
             lastNames.Add(linesLast[i]);
         }
 
         GenerateName ();
         GenerateStats ();
 
     } 
 
     string GenerateName()
     {
         firstName = firstNames [Random.Range (0, firstNames.Count)];
         lastName = lastNames [Random.Range (0, lastNames.Count)];
         
         fullName = firstName + " " + lastName;
         Debug.Log(position);
         return fullName;
     }
     public void GenerateStats()
     {
         //This creates extra points to be allocated to the player's base stats based on
         //how many stars they have been ranked. More stars = more points = better stats
         switch (stars)
         {
         case 5:
             points = 50f;
             break;

         case 4:
             points = 40f;
             break;

         case 3:
             points = 30f;
             break;

         case 2:
             points = 20f;
             break;

         default:
             points = 10f;
             break;
         }
 
         Debug.Log (position);
         //Based on the position, the above extra points will be allocated to the appropriate position
         switch(position)
         {
         case PositionEnum.QB:
             strength = strength + ((30f * points) / 100f);
             acceleration = acceleration + ((10f * points) / 100f);
             speed = speed + ((10f * points) / 100f);
             agility = agility +((25f * points) / 100f);
             endurance = endurance +((25f * points) / 100f);
             break;

         case PositionEnum.RB:
             strength = strength + ((25f * points) / 100f);
             acceleration = acceleration + ((20f * points) / 100f);
             speed = speed + ((20f * points) / 100f);
             agility = agility +((20f * points) / 100f);
             endurance = endurance +((15f * points) / 100f);
             break;

         case PositionEnum.WR:
             strength = strength + ((15f * points) / 100f);
             acceleration = acceleration + ((15f * points) / 100f);
             speed = speed + ((30f * points) / 100f);
             agility = agility +((30f * points) / 100f);
             endurance = endurance +((10f * points) / 100f);
             break;

         case PositionEnum.TE:
             strength = strength + ((40f * points) / 100f);
             acceleration = acceleration + ((20f * points) / 100f);
             speed = speed + ((10f * points) / 100f);
             agility = agility +((15f * points) / 100f);
             endurance = endurance +((15f * points) / 100f);
             break;

         case PositionEnum.OL:
             strength = strength + ((40f * points) / 100f);
             acceleration = acceleration + ((20f * points) / 100f);
             speed = speed + ((10f * points) / 100f);
             agility = agility +((10f * points) / 100f);
             endurance = endurance +((20f * points) / 100f);
             break;

         case PositionEnum.DL:
             strength = strength + ((40f * points) / 100f);
             acceleration = acceleration + ((20f * points) / 100f);
             speed = speed + ((10f * points) / 100f);
             agility = agility +((10f * points) / 100f);
             endurance = endurance +((20f * points) / 100f);
             break;

         case PositionEnum.LB:
             strength = strength + ((25f * points) / 100f);
             acceleration = acceleration + ((25f * points) / 100f);
             speed = speed + ((20f * points) / 100f);
             agility = agility +((20f * points) / 100f);
             endurance = endurance +((10f * points) / 100f);
             break;

         case PositionEnum.DB:
             strength = strength + ((15f * points) / 100f);
             acceleration = acceleration + ((15f * points) / 100f);
             speed = speed + ((25f * points) / 100f);
             agility = agility +((25f * points) / 100f);
             endurance = endurance +((20f * points) / 100f);
             break;

         default:
             break;
         }
     }
 }
 
 
 
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Chris_Dlala · Jan 06, 2015 at 04:22 PM

Hi. The problem is you are using the variable name position twice, once as a temporary variable within public NPCconstructor(int pos, int sta) and once in as a member variable for the class. So change Line 28 to position = (PositionEnum) pos;

Is each instance of the class NPCconstructor a new NPC? Because, your naming is slightly confusing.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image the_big_jablonski · Jan 06, 2015 at 05:47 PM 0
Share

Yes, that was it! Thank you for answering! I thought that was just the syntax for typecasting because of examples I saw on the web. I understand now.

Is each instance of the class NPCconstructor a new NPC?

Yes, that's correct, each instance is a new NPC. This is like my third attempt at a script to accomplish this task so the nomenclature is a little fuzzy. Other than rena$$anonymous$$g the class, do you have any other recommendations for cleaning up my code? I'm still an amateur coder and will take any advice I can get.

avatar image NoseKills · Jan 06, 2015 at 08:44 PM 0
Share

Click the checkmark next to the question to mark this question as answered @Chris_Dlala. Otherwise tons of people like me will end up in this already answered question because it still shows as unanswered

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Single enum class 1 Answer

Code design issue 3 Answers

how to get enum value by looking for their hashcode? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges