Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TaciturnScream · Feb 13, 2013 at 11:53 PM · errorinstantiateprefabgetcomponent

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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".)

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TaciturnScream · Feb 14, 2013 at 12:44 AM 0
Share

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.

avatar image Eric5h5 · Feb 14, 2013 at 12:50 AM 0
Share

Start with this:

 var objects = GameObject.FindGameObjectsWithTag("SpawnPoint");
 vecArray = new Vector3[objects.Length];
 for (var i = 0; i < objects.Length; i++) {...
avatar image TaciturnScream · Feb 14, 2013 at 01:01 AM 0
Share

Oh wonderful. Works like a charm now. Just to tweak a few things now. Thank you so much.

avatar image TaciturnScream · Feb 14, 2013 at 01:02 AM 0
Share

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.

avatar image Eric5h5 · Feb 14, 2013 at 01:21 AM 0
Share

"Top of the screen" is relative; it depends on where your camera is and where it's pointing.

Show more comments
avatar image
0

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 14, 2013 at 12:07 AM 0
Share

The code isn't Boo. It's Unityscript.

avatar image DMCH · Feb 14, 2013 at 12:11 AM 0
Share

Not familiar with that either. I sit corrected!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges