- Home /
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[i].GetComponent().FireWeapon(weaponHardpoint[i]);
}
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;
}
}
I'm struggling to understand your question.
$$anonymous$$ain Script :
var monsters : $$anonymous$$onsterScript[]; // an array that is typecast to the name of a script
function Start()
{
monsters = new monsters[1];
monsters[0] = GameObject.Find( "$$anonymous$$onster_1" ).GetComponent( $$anonymous$$onsterScript );
}
function Update()
{
if ( Input.Get$$anonymous$$ouseButtonDown(0) )
{
monsters[0].DoStuff();
}
}
$$anonymous$$onsterScript :
function DoStuff()
{
Debug.Log( "Doing Stuff !" );
}
Sorry, my code didn't make it in on the first try.
To rephrase, I'm making a new child on the player's ship. Then I need to load the weapon into an array so it can be fired without looking up the weapon again. Because the weapon has important stats on it, I need that specific instance of the weapon, not just the script.
The result is always null.
I hope that clears it up a bit.
Perhaps I should add a unique tag to each weapon and just use FindByTag?
Attempted to mirror your code, but received "WeaponStats is a type but is used like a variable" error
Answer by AlucardJay · May 19, 2013 at 03:55 AM
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[i].GetComponent().FireWeapon(weaponHardpoint[i]); // a reference to the script is stored, just call the function
weaponBay[i].FireWeapon(weaponHardpoint[i]);
}
}
}
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.
Sorry, I was trying to do that earlier by manually adding the ` tags.
But I got that issue solved:
weaponBay[g] = GameObject.Find(weapon.name).GetComponent("WeaponStats") as WeaponStats;
The issue stemmed from returning GameObject when it was wanting WeaponStats.
Thanks for the help though! Now I just need to figure out why my other array is blowing up. That's a problem for another day though.
You already have a reference to the gameObject when you instantiated it, you don't need to then find it before GetComponent :
weaponBay[g] = GameObject.Find(weapon.name).GetComponent("WeaponStats") as WeaponStats;
is the same as :
weaponBay[g] = weapon.GetComponent< WeaponStats >();
Try to avoid using strings if possible.
http://docs.unity3d.com/Documentation/$$anonymous$$anual/GenericFunctions.html
Ah, yeah, that's way easier than recasting the GameObject. Thanks!
Your answer
Follow this Question
Related Questions
How can I get every other GameObject except for the one I'm using? 1 Answer
Finding the angle between two GameObjects 1 Answer
Find 2D Texture via Script to use in Static Function 2 Answers
Function to locate game object with smaller distance to target makes the game lag - UnityScript 1 Answer
Locate a game object using rays 1 Answer