• 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
Question by daclu_10 · Jun 19, 2014 at 12:48 AM · collisioninstantiateprefabsinvokerepeating

instantiate prefabs/gameObjects

Hi, im developing a 2d game.

im using t$$anonymous$$s code to invoke three different kind of objects ramdomly to the scene.

 #pragma strict
 
 var posible_col = new Array();
 
 var col : GameObject;
 
 
 
 function Start()
 {
 
     posible_col[0] = GameObject.Find ("f_col_1");
     posible_col[1] = GameObject.Find ("f_col_2");
     posible_col[2] = GameObject.Find ("f_col_3");
     
         
     InvokeRepeating("next_col", 2, 3);
 
 }                                             
 
 
 
 function next_col ()
 
 {
     col = posible_col[Random.Range(0,3)];
     
     Instantiate(col, new Vector2(0 , 9), Quaternion.identity);
 
 }


it works fine, but it does not instantiate a prefab! i can see at the inspector that the object invoked is not a prefab because the cube besides the drop-down tags option, is not blue but green, blue and red (the components of the object invoked are the same as the prefab!).

im using another script that has a OnCollisionEnter2D funtion on it, it works untill i use t$$anonymous$$s one. And the tests i've made so far, show that t$$anonymous$$s collision script only works when the colliders are prefabs, i dont know why, but when two of these invoked objects collide, apearly it does not detect the collision and so not$$anonymous$$ng happens.

the names i used when GameObject.Find are the names of my prefabs, ¿why it instantiate an object? ¿is there somet$$anonymous$$ng wrong about t$$anonymous$$s code i've posted, that could interfere with the other one?

thanks for your help!

Comment
dimosdennis

People who like this

1 Show 1
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 Jeff-Kesselman · Jun 19, 2014 at 12:52 AM 0
Share

First off, if you dont use a proper code block then no one can help you.

I fixed it this time but please post your code properly formatted in the future.,

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jeff-Kesselman · Jun 19, 2014 at 12:54 AM

Instantiate makes a clone of an object. T$$anonymous$$s is an object that is an exact copy with all the same components and settings.

In the special case that it is given a prefab, it makes a prefab instance,

You did not give it a prefab, however. You gave it a prefab-instance from your $$anonymous$$rearchy, w$$anonymous$$ch as far as it is concerned is just an object to clone.

Comment
dimosdennis

People who like this

1 Show 3 · 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 Jeff-Kesselman · Jun 19, 2014 at 01:15 AM 0
Share

OP responded: thank you so much!

so i must write in the code a different way to find it?

or what do i have to do?

they are in a folder called "prefabs".

avatar image Jeff-Kesselman · Jun 19, 2014 at 01:17 AM 0
Share

You have two choices.

(1) Declare a public GameObject field in your script to hold the prefab and set it in the editor. http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

(2) Use Resources.Load to load the prefab from your assets. http://docs.unity3d.com/ScriptReference/Resources.Load.html

avatar image daclu_10 · Jun 19, 2014 at 03:22 PM 0
Share

Hi againg!

i've changed all the prefabs to the Resources folder, and im now using the Resources.Load. ("prefabs/col1"); to load each one of the objects, it instantiate them without errors, but i still dont get them to be prefabs. what else i need to do?

i dont understand how to declare a public GameObject and set it to the editor, didn´t found it where you so kindly pointed.

Thanks a lot!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prevent Overlap with Prefabs Instantiating themselves 1 Answer

How to use many particle systems, just varying in size and emitter dimensions 0 Answers

OnCollisionEnter doesn't work if enemies and allies have rigidbody ? 1 Answer

How to apply check on an instanced prefab 2 Answers

Glitch/overlapping when two objects are on exactly same transform 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