Reputation: 171
I have an issue where my prefab gun shows up as inactive in my hierarchy after I instantiated it.
void IInteractableTarget.Interact(GameObject character)
{
int winningInt = Random.Range(1, 3);
if(winningInt == 1)
{
GameObject obj_rifle = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
}
if (winningInt == 2)
{
GameObject obj_pistol = Instantiate(pistolPrefab, spawnPos.position, spawnPos.rotation);
}
//GameObject obj = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
//obj.GetComponent<Rigidbody>().AddForce(transform.up * 5, ForceMode.Impulse);
//Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
Debug.Log("Weapon from chest instantiated" + " " + winningInt);
}
How should I handle this?
Upvotes: 1
Views: 1365
Reputation: 757
From the documentation https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
The active status of a GameObject at the time of cloning is maintained, so if the original is inactive the clone is created in an inactive state too.
You can just set the active status of the GameObjects to true
void IInteractableTarget.Interact(GameObject character)
{
int winningInt = Random.Range(1, 3);
if(winningInt == 1)
{
GameObject obj_rifle = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
obj_rifle.SetActive(true); // <-- Set active to true
}
if (winningInt == 2)
{
GameObject obj_pistol = Instantiate(pistolPrefab, spawnPos.position, spawnPos.rotation);
obj_pistol.SetActive(true); // <-- Set active to true
}
//GameObject obj = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
//obj.GetComponent<Rigidbody>().AddForce(transform.up * 5, ForceMode.Impulse);
//Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
Debug.Log("Weapon from chest instantiated" + " " + winningInt);
}
Upvotes: 1