Hey, I’ve been searching for a solution to this for a while, but I can’t seem to find quite what I’m looking for.
I’m trying to loop through a script loaded on other objects and load the scripts into arrays to use later on. The problem is that GameObject.Find wants to use the function immediately, and won’t store WeaponStats for future use. I’ve tried GameObject.Find() and transform.FindChild() without success. Obviously GameObject isn’t going to work, but I get even more errors if I cast it as anything else. There must be a way to do this without declaring another variable or using a Find function on update (would destroy performance).
Or do I have to use GameObject.Find(object); and then call GetComponent?
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private WeaponStats[] weaponBay = new WeaponStats[3];
void Start(){
weaponBay = new weaponBay[3];
int g = 0;
while(g <= 2){
GameObject weapon = Instantiate(Resources.Load("Weapons/Smallcannon"), transform.position, transform.rotation) as GameObject;
weapon.transform.parent = transform;
weapon.name = "weaponLoad" + g;
weaponBay[g] = GameObject.Find(weapon.name);
g++;
}
}
void FireWeapons(){
int i = 0;
while(i <= 2){
weaponBay_.GetComponent().FireWeapon(weaponHardpoint*);*_
* }*
* public void FireWeapon(Vector3 firePoint){*
* if(Time.time >= loadWeapon){*
* loadWeapon = Time.time + 60 / rateOfFire;*
* //Fire Projectile *
* //cannonVector = new Vector3(Vector3(fir);*
* audio.PlayOneShot(soundFX); *
* Projectile zProjectile = Instantiate (projectile, firePoint, Quaternion.identity) as Projectile;*
* //Pass weapon stats on to projectile*
* zProjectile.damage = Random.Range(weaponMinDamage,weaponMaxDamage);*
* zProjectile.ion = Random.Range(weaponMinIon,weaponMaxIon);*
* zProjectile.velocity = projectileVelocity;*
* zProjectile.attackType = attackType;*
* }*
* }*