Loading function into array using GameObject.Find

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;*
* }*

* }*

You already have a reference to the gameObject when you instantiate it. From that gameObject, use GetComponent to store a reference to the script in your array, then you can call the function on that script :

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); // you already have a reference to the gameObject (weapon) when you instantiated it, here you want to store a reference to the script
			weaponBay[g] = weapon.GetComponent< WeaponStats >();
			g++;
		}
	}

	void FireWeapons(){

		int i = 0;
		while(i <= 2){
			//weaponBay_.GetComponent().FireWeapon(weaponHardpoint*); // a reference to the script is stored, just call the function*_

weaponBay_.FireWeapon(weaponHardpoint*);
}
}
}*

Function you’re trying to reference :
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;*
* }*
}
C# is not my native language, but this seems logically correct.
*http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html*_
--------------------------------
Please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
* Read this page : http://answers.unity3d.com/page/newuser.html_

_* Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers_