- Home /
Creating class and add property to GameObject
i have Enemy Vehicles more than a hundred. They must be loaded scene by Level and Wave. Each Vehicle has different point, if an enemy cross line it will hit me that point. i tried to create a class but it didnt work. i dont know how to call them with order and use properties
using UnityEngine; public class EnemyVehicles : MonoBehaviour { public class Vehicles { public GameObject V; public float point;
public Vehicles(GameObject go, float p)
{
V = go;
point = p;
}
}
private Vehicles[] Ve;
public GameObject[] go;
public float[] p;
void Start()
{
for (int i = 0; i < go.Length; i++)
{
Ve[i].V = go[i]; //it doesnt work
Ve[i].point = p[i];
}
Debug.Log(Ve[0].V.transform.name + Ve[0].point);
}
public GameObject GetVehicle(int i)
{
return Ve[i].V;
}
}
Answer by mesuttasci · Oct 20, 2018 at 11:12 PM
i solve my problem
using UnityEngine;
public class EnemyVehicles : MonoBehaviour { public class Vehicles { public GameObject Vehicle; public float point;
public Vehicles(GameObject go, float p)
{
Vehicle = go;
point = p;
}
public Vehicles(GameObject go)
{
Vehicle = go;
point = 1;
}
}
public Vehicles[] Ve = new Vehicles[5];
public Vehicles Veh;
public GameObject[] go;
public float[] p;
void Start()
{
for (int i = 0; i < go.Length; i++)
{
Veh = new Vehicles(go[i], p[i]);
Ve[i] = Veh;
Debug.Log(Ve[i].Vehicle.name + " :"+Ve[i].point);
}
}
public GameObject GetVehicle(int i)
{
return go[i];
}
public float GetPoint(int i)
{
return p[i];
}
}
Answer by mesuttasci · Oct 20, 2018 at 09:42 PM
i convert array to list but it didnt work
public List<Vehicles> Ve;
public Vehicles Veh;
public GameObject[] go;
public float[] p;
void Start()
{
for (int i = 0; i < go.Length; i++)
{
Veh.V = go[i];
Veh.point = p[i];
Ve.Add(Veh);
Debug.Log(Ve[i].V.name + " :"+Ve[i].point);
}
}
Your answer
Follow this Question
Related Questions
How do I check if a derived class is a certain class? 1 Answer
Object is null when it shouldn't be. 1 Answer
making Turrets 0 Answers
Call a function of a custom class through a SerializedProperty 0 Answers
[solved] Problem getting data from array 0 Answers