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

instantiate prefabs/gameObjects

Hi, im developing a 2d game.

im using this 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 this one. And the tests i've made so far, show that this 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 nothing happens.

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

thanks for your help!

Comment
Add comment · 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
1
Best Answer

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

Instantiate makes a clone of an object. This 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 hirearchy, which as far as it is concerned is just an object to clone.

Comment
Add comment · 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/$$anonymous$$anual/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.

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

Stop instantiate on collision from other script 1 Answer

Cannot cast from source type to destination type when trying to add Prefab instantiate to a list 0 Answers

GameObject instances affecting each other 0 Answers

Instantiating Multiple Prefabs at Runtime 0 Answers


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