- Home /
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
,
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"));
}
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. Do you know where it went wrong?
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
Answer by krupps · Jan 05, 2021 at 01:21 PM
Take a look at this https://learn.unity.com/project/2d-roguelike-tutorial
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.
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"));
}
@xxmariofer I tried it. But it doesn't work for me. When I click on the ui button my cube does not appear.
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
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.
Your answer
Follow this Question
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