- Home /
Instantiate Prefabs. Errors.
Hey all, I'm working on a game for a class and I'm so stuck. I want to spawn objects every so often from a random array of points. This is the code that I have so far and I keep getting the error:
MissingMethodException: Method not found: 'UnityEngine.GameObject[].GetComponent'. Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher () Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create () Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey14.<>m_7 () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) Spawn.Start () (at Assets/Scripts/Spawn.js:5)
No idea. This is the code though(it's from another source but I can't find anything else about it or how to fix it):
var vecArray:Vector3[];
var top:int;
var Rectangle:Transform;
function Start(){
vecArray = GameObject.FindGameObjectsWithTag("SpawnPoint").GetComponent(Transform).position;
top = vecArray.Length;
InvokeRepeating("CreateObject",01f,3.0f);
}
function CreateObject(){
Instantiate(Rectangle,vecArray[Random.Range(0,top)],Quaternion.identity);
}
Answer by Eric5h5 · Feb 13, 2013 at 11:58 PM
FindGameObjectsWithTag returns an array of GameObjects, so your method of assigning vecArray can't work--Transform.position is not a property of an array. What you should do instead is create a Vector3 array with the same number of elements as the array returned by FindGameObjectsWithTag, then iterate through the GameObject array and get the transform.position from each one, and assign that to the appropriate slot in the Vector3 array. (Also you don't need "GetComponent(Transform)", you can just write "transform".)
I really don't understand. This is my first attempt at using Unity and the only other code experience I have is with Action Script 3 (which is very $$anonymous$$imal as well). Any other information you could lend would be quite helpful.
Start with this:
var objects = GameObject.FindGameObjectsWithTag("SpawnPoint");
vecArray = new Vector3[objects.Length];
for (var i = 0; i < objects.Length; i++) {...
Oh wonderful. Works like a charm now. Just to tweak a few things now. Thank you so much.
Actually, do you have any idea how I might go about moving the point at which they spawn? Right now they're spawning in the middle of the screen and I would like them to spawn at the top.
"Top of the screen" is relative; it depends on where your camera is and where it's pointing.
Answer by DMCH · Feb 14, 2013 at 12:03 AM
Hello,
I'm not familiar with Boo, but I wrote some similar code in C#. Might be of some use
This will get any Gameobjects Tagged as "SpawnPoint"
// Initialise the array with 20 elements
spawnPointArray = new GameObject[20];
// Populate the array with the Spawn Point Gameobjects
spawnPointArray = (GameObject[])GameObject.FindGameObjectsWithTag("SpawnPoint");
This will spawn (or instantiate) the Prefab enemy at the point. You could set up a timer in update, put what's below in a method, and call it when the timer expires.
// Get a random number and access that position on the spawn point array
int spawnPointNumber = Random.Range(1, 20);
Vector3 activeSpawn = spawnPointArray[spawnPointNumber].transform.position;
// Instantiate the enemy
Instantiate(enemy, activeSpawn, Quaternion.identity);
Your answer
Follow this Question
Related Questions
Play iTween on instantiated prefab 1 Answer
Access a function of an instantiated prefab 1 Answer
disabling a script when collide with a cube 3 Answers
Setting the parent of a transform which resides in a prefab error 1 Answer
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer