I’m trying to get a position variable for an object in lets say…an enemy class;
how can i use the transform component to get the x,y,z position of the enemy object?
Public Class Enemy{
Public GameObject eObject;
Public float enemyXpos;
Public float enemyYpos;
public float enemyZpos;
public int health;
Public Enemy(x,y,z,hp)
{
enemyXpos = x; ????
enemyYpos = y; ????
enmeyZpos = z; ????
health = hp
}
}
Public Class Manager{
Enemy enemy = new Enemy(<enemyXpos> ,<enemyYpos> ,<enemyZpos>,<health> );
void Start(){
}
void Awake(){
}
}
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public int Health;
public GameObject eObject;
public Transform ePosX = eObject.transform.position.x; ???
public Transform ePosY = eObject.transform.position.y; ???
public Transform ePosZ = eObject.transform.position.z; ???
Transform transform;
void Awake()
{
transform = GetComponent<Transform>();
}
public Enemy( ?var x, ?var y , ?var z int hp)
{
ePosX = x;
ePosY = y;
ePosZ = z;
Health = hp;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
No, you cant set fields like that, your syntax is all wrong, and i am not sure on exactly what you are trying to do.
With the limited information you gave, why can you not simply do this?
public class Enemy : MonoBehaviour {
public int Health;
public GameObject eObject;
private float xPos, yPos, Zpos;
void Start()
{
xPos = eObject.transform.position.x;
yPos = eObject.transform.position.y;
Zpos = eObject.transform.position.z;
}
}
Assuming you want to get the positions of the eObject GameObject.
Maybe this is what you are trying to achieve, but i am just guessing
public class Enemy
{
public GameObject eObject;
public float enemyXpos;
public float enemyYpos;
public float enemyZpos;
public int health;
public Enemy(GameObject go, Transform pos, int hp)
{
eObject = go;
enemyXpos = pos.position.x;
enemyYpos = pos.position.y;
enemyZpos = pos.position.z;
health = hp;
}
}
public class Manager : MonoBehaviour
{
private Enemy enemy;
public GameObject enemyGO;
public int health = 100;
void Start()
{
enemy = new Enemy(enemyGO, enemyGO.transform, health);
}
}