Hi,
I’ve been struggling on this for quite some time and would love to get some help on this.
Here it is:
I have three scripts:
public class PlayerOneShooting: MonoBehaviour {
public static GameObject pistol;
public static GameObject pistolBullet;
GunBehaviour GunInHand;
GunBehaviour Pistol = new GunBehaviour(1f, new BulletBehaviour(4f, 8f, 25, pistolBullet), pistol, "PlayerHand1", "Pistol");
void Start()
{
GunInHand = Instantiate
}
public GameObject ConstructGun()
{
GameObject tempGIH = (GameObject)Instantiate(Prefab, GameObject.Find(Playerhand).transform.position, GameObject.Find(Playerhand).transform.rotation);
tempGIH.transform.parent = GameObject.Find(Playerhand).transform;
return tempGIH;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GunInHand.Fire();
}
}
}
public class GunBehaviour : MonoBehaviour {
float Reload;
BulletBehaviour Bullet;
GameObject Prefab;
string Playerhand;
string Name;
public GunBehaviour(float reload,BulletBehaviour bullet,GameObject prefab,string playerhand,string name)
{
Reload = reload;
Bullet = bullet;
Prefab = prefab;
Playerhand = playerhand;
Name = name;
}
public GameObject Construct()
{
GameObject tempGIH = (GameObject)Instantiate(Prefab, GameObject.Find(Playerhand).transform.position, GameObject.Find(Playerhand).transform.rotation);
tempGIH.transform.parent = GameObject.Find(Playerhand).transform;
return tempGIH;
}
public void Fire()
{
BulletBehaviour COB = Instantiate(Bullet);
COB.Construct(Prefab.transform.GetChild(0));
}
}
public class BulletBehaviour : MonoBehaviour {
float Lifetime;
float Speed;
int Power;
GameObject Prefab;
GameObject Obj;
public BulletBehaviour(float lifetime,float speed, int power, GameObject prefab)
{
Lifetime = lifetime;
Speed = speed;
Power = power;
Prefab = prefab;
}
void Awake()
{
Destroy(Obj, Lifetime);
}
void Update()
{
Obj.transform.Translate(Vector3.forward * Speed * Time.deltaTime);
}
public void Construct(Transform spawn)
{
Obj = (GameObject) Instantiate(Prefab, spawn.position, spawn.rotation);
}
}
There is a fundamental issue here, something I believe about not being able to create new objects of Guns that use monobehaviour. How do I solve.