I'm new to coding games, so my logic may not be right for creating stats.
Quick intro: I'm creating a game that spawns units at the players command that go fight other units. To store all the different units stats I am creating a statsheet then (trying) to search that stat sheet in the units entity script that defines everything about that unit. I have an entity prefab that has all the scripts that apply to all units (such as move and damage). Then after the GameManager spawns the entity prefab, it builds another prefab (which is the actual unit) and moves it as a child inside the entity prefab.
I'm trying to add stats to each unit so I built a statsheet that holds non-MonoBehaviour classes with the different unit stats. The entity script then takes the name of the child(which is the unit name) and subtracts "(clone)" from it, then I want it to search for that class inside the statsheet based on that unit name and then assign the stats based of the preset stats in the statsheet. I don't know if this is the best way, or even a possible way at all. But so far logically it's the best I can come up with. I'm open to any suggestions if this won't work, however, the issue I'm running into with my current method is I can't search a class in another script based off a string. I've been searching the web for about 2 hours now and can't find anything.
entity script snippet:
public class Entity : MonoBehaviour
{
public Movement movement;
public int health;
public int damage;
public int attackSpeed;
public int range;
public int moveSpeed;
public bool flying;
public int cost;
private void Start()
{
movement = GetComponent<Movement>();
string unitName = transform.GetChild(0).name;
unitName = unitName.Remove(unitName.Length-7);
//unitName for this example returns "Pen1"
health = unitName.health;
}
here is my statsheet snippet:
public class Pen1
{
public int health = 50;
public int damage= 5;
public int attackSpeed = 1;
public int range = 1;
public int moveSpeed = 1;
public bool flying = false;
public int cost = 50;
}
CodePudding user response:
I'm not the most experienced in game development but my suggestion is to avoid everything your doing. If your making a prefab for each unit already, just add the scripts you have in the UnitEntity to your unit prefab and do away with any code related to unitprefab and replace it with your actual unit. Move your statsheet values without given values into your entity script. Then inside your prefab set all your values there. When you load your prefab you won't need a statsheet bc the units stats will be set in the prefab.
CodePudding user response:
Searching by class name does not make sense because your class name may not have a health attribute. Then it will error the compiler. You need another logic. If you want to have different entities in the game, all you have to do is inherit them from the Entity class and then customize the variables. Finally prefab them and you can Instantiate whenever you want. You also do not need a clone.
public class Pen1 : Entity
{
// setup variable in Inspector then prefab it.
public void Write() // special method in pen that other entities doesn't have
{
// do something
}
}
Now you can do the following to find pen classes that have their own properties. Remember that class definitions actually cover types, not just definitions of different instances. So even pens that have the same method properties and fields do not need to define another class.
var pens = FindObjectsOfType<Pen1>(); // it returns a list of pens
foreach (var p in pens)
{
p.Write();
Debug.Log(p.health);
}