How to Access Values from a Object,How to access values from a object
Hi. I have a issue to access values from a object attached a prefab.
To better understanding i will put here some scripts:
//SpawnerManager.cs
using UnityEngine;
public class SpawnerManager : MonoBehaviour
{
public Transform spawnPoint;
public void SpawnEnemy(GameObject Enemy){
Instantiate(Enemy.transform, spawnPoint.position, spawnPoint.rotation);
}
}
Human.cs is the Object. I have one called Spider with the same attributes but different Values.
//Human.cs
using UnityEngine;
public class Human : MonoBehaviour {
public static GameObject Prefab;
static int hp = 100, goldValue = 1;
static float speed = 10f;
Enemy Inimigo = new Enemy(hp, goldValue, speed, Prefab);
}
Enemy.cs is the class for the objects Human and Spider: using UnityEngine;
public class Enemy
{
int hp, goldValue;
float speed;
GameObject teste;
public Enemy(int _hp, int _goldValue, float _speed, GameObject _teste)
{
//this is a constructor for the class. When a new Vehicle is created,
//you can use this constructor to execute the following code
hp = _hp;
goldValue = _goldValue;
speed = _speed;
teste = _teste;
}
}
So, i have two buttons in a panel, these buttons when clicked they call the function SpawnEnemy() from SpawnerManager. The Enemy is Spawning because i attached the Prefab Human / Spider. In the Human Prefab i have attached the Human.cs and Spider Prefab, Spider.cs.
So my question is, How can i get the value from the Object Human.cs or Spider.cs in the SpawnEnemy()?
,Hi. I have a problem to access values from a object.
I have a SpawnerManager :
using UnityEngine;
public class SpawnerManager : MonoBehaviour
{
public Transform spawnPoint;
public void SpawnEnemy(GameObject Enemy){
Instantiate(Enemy.transform, spawnPoint.position, spawnPoint.rotation);
}
}
And i have a Panel with 2 buttons calling the function SpawnEnemy from the SpawnerManager. 1 should spawn a Human Enemy and another button should spawn spider enemy. But this is not my issue, each Enemy Prefab (Human and Spider) is a object like :
using UnityEngine;
public class Human : MonoBehaviour {
public static GameObject Prefab;
static int hp = 100, goldValue = 1;
static float speed = 10f;
Enemy Inimigo = new Enemy(hp, goldValue, speed, Prefab);
}
and the Enemy which is the type:
using UnityEngine;
public class Enemy
{
int hp, goldValue;
float speed;
GameObject teste;
public Enemy(int _hp, int _goldValue, float _speed, GameObject _teste)
{
//this is a constructor for the class. When a new Vehicle is created,
//you can use this constructor to execute the following code
hp = _hp;
goldValue = _goldValue;
speed = _speed;
teste = _teste;
}
}
Okay. After all, to resume my question: How can i get the values from the objects like (hp, goldValue ...) in the SpawnerManager?
Thank you.
Your answer
Follow this Question
Related Questions
Problem with int values + 1 gives unexpected values 1 Answer
How do I prevent the destroying of all instances of an object in Unity? 0 Answers
Object to GameObject casting error c# (from object in scene not a prefab) 0 Answers
Temporary Gameobject Reference in Function without Instantiating 2 Answers
How do I pass in a GameObject as a parameter when I'm instantiating a class? 0 Answers