- Home /
Using GUI Button to activate script instances
I am completely new to scripting of any sort and I have the following script set up to pull in rigidbody prefabs:
var spawnObject : Rigidbody;
function spawner () {
var newObject: Rigidbody = Instantiate (spawnObject, transform.position, transform.rotation);
}
I have attached the script to two seperate empty game objects - BallSpawnpoint and CubeSpawnpoint.
I am now wanting to activate both of these from a single GUI button with the following script:
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "Drop")) {
GetComponent(spawn.spawner());
}
}
which is returning the following error:
member 'spawner'.
member 'spawner'.> Assets/scripts/Button.js(7,36):
BCE0020: An instance of type 'spawn' is required to access non static member 'spawner'.member 'spawner'.
Can someone help please?
Answer by skovacs1 · Oct 21, 2010 at 05:32 PM
You cannot access the function spawner because your script doesn't know where to get it from.
Also GetComponent gets a component attached to the specified gameobject (in this case the script to which your GUI script is attached) of the type specified. A function is not a type of component.
If your function were static, you could access the function spawn.spawner (static function spawner in the script spawn.js), but the variable transform would be invalid to use within the static function because it is not static (as would spawnObject, but depending on your use case is less problematic).
Because you want to call the function on instances of the script, you will need to get the instance(s) of the script attached to your other GameObjects like so:
button.js
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "Drop")) {
//Find the spawners. Requires they be tagged Spawner
//If these don't change, you could store this on Start
//or you could store them publicly (and even statically if you like).
var spawners : GameObject[] =
GameObject.FindGameObjectsWithTag("Spawners");
for(var spawner : GameObject in spawners) {
var script : spawn = spawner.GetComponent(spawn);
if(script) //If they have this script attached call the function
spawn.spawner();
}
}
}
As an alternative, depending on your use case, in stead of having the spawner function in a script attached to your spawner GameObjects, you could store the GameObjects and the function within the button or another controller script that is easier to access (maybe as a static function?) and then have the spawner function loop through the GameObjects that are stored with it and instantiate, like so:
spawnTarget.js (Attached to your spawners)
var spawnObject : Rigidbody;
spawn.js (Doesn't really need to be attached to anything, but it can be)
static var spawners GameObject[];
static function spawner() { for(var spawner : GameObject in spawners) { var script : spawnTarget = spawner.GetComponent(spawnTarget); if(script) { var target : Rigidbody = script.spawnObject; var newObject: Rigidbody = Instantiate(target, spawner.transform.position, spawner.transform.rotation); } } }
button.js (Attached to something)
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "Drop")) spawn.spawner();
}
I have tried copying in the first script and I'm getting the following errors:
Assets/scripts/Button.js(6,61): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'.
and
Assets/scripts/Button.js(10,23): BCE0020: An instance of type 'spawn' is required to access non static member 'spawner'.
Sorry. was calling the wrong version of FindWithTag. It has been corrected.