- Home /
The question is answered, right answer was accepted
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;
}
}
}
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.
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.
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
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