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 thendral30 · Jan 05, 2021 at 12:30 PM · instantiateinstantiate prefab

How to instantiate a prefab each time I click the UI button? (getting it only once when I click the button)

In my scene - I have created a ui button on a panel. I have attached my cube prefab on this button and have also written a script to instantiate the prefab. The problem I have is when I click on the button the cube appears. But if I click on it again it does not appear. I want the cube to appear each time I click on the button. Can any one help me with this? I am new to unity will be great if I could get some help.

[1]: /storage/temp/173624-button.png

,

button.png (73.9 kB)
code.png (51.1 kB)
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 xxmariofer · Jan 05, 2021 at 12:54 PM 0
Share

you need to add a new method, and change the setactive method from the button to this one

 public void InstantiateGameObject(GameObject _object)
 {
    Instantiate(Resources.Load("mycubePrefab"));
 }

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by thendral30 · Jan 05, 2021 at 05:13 PM

@bguyl I tried what you said but when I click on the button now my cube doesnt appear.alt text Do you know where it went wrong?


script.png (27.5 kB)
prefab-button.png (41.4 kB)
Comment
Add comment · Show 1 · 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 bguyl · Jan 06, 2021 at 08:05 AM 0
Share

It should work. Is your cube is present in your hierarchy folder ? (Named mycubePrefab (Clone))

If you want to control where the cube is spawned, use this :

 public class Instantiator :
 $$anonymous$$onoBehaviour {
     public Transform _spawnPostion;
     public GameObject _prefab;
 
     public void InstantiateCaller()
     {
         Instantiate(_prefab, transform);
     }
 }

Also, don't use the same name for component field and function argument. Here, you used twice mycubePrefab but they are not the same value. Also, you need only one of these statements.

Notice that in my 2nd example, I used component field instead of function argument

avatar image
0

Answer by krupps · Jan 05, 2021 at 01:21 PM

Take a look at this https://learn.unity.com/project/2d-roguelike-tutorial

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 thendral30 · Jan 05, 2021 at 02:23 PM 0
Share

Hello! I am working in unity 3d with oculus quest. I want to click a button and load a prefab(which I got). I want to click the button again and another copy of prefab must be loaded.

avatar image xxmariofer thendral30 · Jan 05, 2021 at 02:36 PM 0
Share

you need to add a new method, and change the setactive method from the button to this one

 public void InstantiateGameObject(GameObject _object)
 {
    Instantiate(Resources.Load("mycubePrefab"));
 }
avatar image thendral30 xxmariofer · Jan 05, 2021 at 06:27 PM 0
Share

@xxmariofer I tried it. But it doesn't work for me. When I click on the ui button my cube does not appear.

avatar image
0

Answer by bguyl · Jan 05, 2021 at 03:40 PM

1 - Create a MonoBehavior script that you will attach to your button

2 - Create an Instanciate(prefab) methods that will be called from the OnClick UnityEvent, something like this:

 public class Instantiator : MonoBehaviour
 {
     public void InstantiateCaller(GameObject prefab)
     {
         Instantiate(prefab);
     }
 }

3 - From your button, call this method instead of GameObject.SetActive and drag and drop your prefab from the Project folder and not from the scene in the argument part. Also, remove the prefab you already have in your scene, it's misleading

Comment
Add comment · 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
0

Answer by tkati · Jan 06, 2021 at 05:17 PM

I'd also be careful what the transform is for instantiated objects. If the transform/position isn't changing, you could just be creating objects on top on one another so it just appears they aren't being created. Easiest way to check to see if an object was instantiated is to check the hierarchy when game is running.

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

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

114 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Projectiles trigger affecting other projectiles of same prefab - C# 0 Answers

JS: How to instantiate a prefab from a public class? 1 Answer

Spawning prefabs in front of the player 1 Answer

Trying to instantiate a ball at the cursor position 1 Answer

Show GUI Texture on Trigger and Instantiate Object if Button pressed while In Trigger? 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