I’ve been writing a new little project recently and I’ve gotten stuck on this bit. Instead of writing the newEntities stats as the stats on the list and leaving the values of the list alone, it updates the values of the list in real-time as the game is being played. Directly below is the only reference to the list I have within the project currently.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World_Manager : MonoBehaviour
{
public List<Entity> activeEntities;
void Start()
{
SpawnEntity("Player");
SpawnEntity("Enemy");
SpawnEntity("Enemy");
SpawnEntity("Enemy");
SpawnEntity("Enemy");
SpawnEntity("Enemy");
}
public void SpawnEntity(string entityName)
{
List<Entity> entityList = GameObject.Find("Data Manager").GetComponent<EntityList>().entityList;
for (int i = 0; i < entityList.Count; i++)
{
if (entityName == entityList*.entityName)*
{
GameObject newEntity = Instantiate(entityList*.entityObject, new Vector2(Random.Range(-10, 10), Random.Range(-4, 4)), Quaternion.identity);*
newEntity.GetComponent().setStats(entityList*);*
AddActiveEntity(newEntity.GetComponent());
return;
}
}
}
public void AddActiveEntity(Statistics newStats)
{
newStats.activeID = activeEntities.Count;
activeEntities.Add(newStats.entityStats);
}
Here is the List itself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EntityList : MonoBehaviour
{
public List entityList = new List();
void Awake()
{
entityList.Add(new Entity(“Player”, 0, new Vector2(0, 0), (GameObject)AssetDatabase.LoadAssetAtPath(“Assets/Prefabs/Entities/Player.Prefab”, typeof(GameObject)), 100, 100));
entityList.Add(new Entity(“Enemy”, 1, new Vector2(0, 0), (GameObject)AssetDatabase.LoadAssetAtPath(“Assets/Prefabs/Entities/Enemy.Prefab”, typeof(GameObject)), 100, 100));
}
}
and here is the Entity class I use to define the lists type
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Entity
{
public string entityName;
public int entityListID;
public Vector2 entityPos;
public GameObject entityObject;
public float entityHealth;
public float entityMana;
public Entity(string entityNme, int listID, Vector2 objPos, GameObject entityObj, float health, float mana)
{
entityName = entityNme;
entityListID = listID;
entityPos = objPos;
entityObject = entityObj;
entityHealth = health;
entityMana = mana;
}
}
Finally, here is the players stats page, which is currently what tells each instantiated gameObject what its stats are
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Statistics : MonoBehaviour
{
public int activeID;
public Entity entityStats;
void Update()
{
entityStats.entityPos = gameObject.transform.position;
}
public void setStats(Entity newEntityStats)
{
entityStats = newEntityStats;
entityStats.entityHealth = newEntityStats.entityHealth * Random.Range(0.4f, 1.7f);
}
}
If anyone can help me out or needs more information let me know. Much appreciated.