- Home /
Instantiate only one prefab out of 3 prefabs on a button click during runtime
hello! Good day! My question is simple.. just not simple enough for me.. as I am still learning to code. I would like some help with my code as I have not been able to find what I need for a few days now. I have 3 prefabs that I need to instantiate, but only 1("CubeA") at a time on a location per button click. And repeat the same for ("CubeB") & ("CubeC") so that they appear one by one per clicks at different places. Following is my code, where at the moment all 3 instantiate the same time, same place on one button click. Thank you so much for your time. Really appreciate any outsider view to hopefully nudge me towards the right path. stuck and stranded on a Wednesday:(
unity2017.4
c#
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.WSA;
using UnityEngine.XR.WSA.Persistence;
using Vuforia;
void Start()
{
Button btn = this.GetComponent<Button>();
if (btn)
{
btn.onClick.AddListener(ToggleARCamera);
btn.onClick.AddListener(Instantiate);
}
void Instantiate ()
{
{
if (active == true )
{
GameObject newGO = Instantiate(Cube, transform.position, transform.rotation);
newGO.transform.parent = TeamCube.transform;
newGO.name = "CubeA";
}
if (active == true)
{
GameObject newGO = Instantiate(Cube, transform.position, transform.rotation);
newGO.transform.parent = TeamCube.transform;
newGO.name = "CubeB";
}
if (active == true)
{
GameObject newGO = Instantiate(Cube, transform.position, transform.rotation);
newGO.transform.parent = TeamCube.transform;
newGO.name = "CubeC";
}
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Problem with iTween and instantiated path 0 Answers
How to get all objects instantiated from a single prefab? 1 Answer
Multiple Cars not working 1 Answer
Clear Does not work to remove 1 Answer