- Home /
Using Enums for property customization
I'm making a shooter were the player will have different weapons and stats based on the current equipment it's wearing but I'm having a hard time figuring out how to do this as I haven't used Enums before.
Below I have three different scripts, the Player, the PlayerProperties, and the StandardArm. I want the Player script to be able to take the speed values from the StandardArm Script and add it to the current speeds using the PlayerProperties as the middle man. The problem is I'm not sure what I need to add in for this to work.
//First Script
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private float horizontalSpeed = 10.0f;
private float riseSpeed = 10.0F;
private float dropSpeed = 10.0F;
private float forwardAddShootingSpeed ;
private float backAddShootingSpeed ;
private float riseAddShootingSpeed ;
private float dropAddShootingSpeed ;
void Update (){
}
}
//Second Script
using UnityEngine;
using System.Collections;
enter code herepublic class PlayerProperties : MonoBehaviour {
enum Weapon {
Standard = 0,
SprayGun = 1,
}
(int)PlayerState.AirFighter;
public int armWeapon = (int)Weapon.Standard;
void CustomizeAirFighter () {
switch (armWeapon)
{
case (int)Weapon.Standard :
print ("Standard Shooting System");
break;
case (int)Weapon.SprayGun :
print ("Spray Gun System");
break;
}
}
}
//Third Script
using UnityEngine;
using System.Collections;
public class StandardArm : MonoBehaviour {
private float forwardShootingSpeed = -7.0F;
private float backShootingSpeed = -4.0F;
private float riseShootingSpeed = -4.0F;
private float dropShootingSpeed = -3.0F;
}
I left out the bulk of the code not necessary to my problem so I hope it's not hard to understand. I basically just want to know how the speed values from Standard Arm Script get fed in to the PlayerProperties script witch then feeds in the currently selected enum value to the player script. I'm using C# but any help would be most appreciated. Thanks.
It looks like what you really want is a Weapon class, with different functionality for each weapon. Doing this with enums is a dirty, dirty hack.
A weapon class huh? Would I be able to switch between different weapons while still using the same properties in my Player class? Forgive me I'm new to program$$anonymous$$g so I'm unsure of how a Weapon class could do this better.
Yes, absolutly. Create a base Weapon class and inherit from it to create the idiosyncrasies for each weapon. $$anonymous$$eep each one on a GameObject and switch out which one is active. This way you can also have different meshes for each weapon as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105434im_/https://answers.unity.com/themes/thub/images/avi.jpg)