- Home /
Instantiate inactive,Making a GameObject active/instantiate
Noob question for my first project.
I want to make a GameObject 'bigBrother' appear when another two objects collide.
I've been trying to do this by having it inactive and using SetActive with an if statement, but I can't make it work and I realised I can't even do this without the If statement, not even with a script that says nothing else but SetActive on Start or Wake or Update... I thought that perhaps it is because Unity can't see objects that are inactive at the start of the game, and some forum posts suggest making the inactive prefab a child of an active empty (https://answers.unity.com/questions/921123/enabling-a-disabled-prefab-in-the-scene.html) but this doesn't seem to make a difference.
So I figure SetActive is not the way and I am trying out Instantiate. This works insofar as it creates a clone in the game but one that is inactive and I can't get it to activate! I have seen examples of code that just say SetActive.'name of prefab' that are supposed to activate the just instantiated clone, but maybe I need to refer to the clone specifically? The in-game name is suffixed with '(clone)'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnBigBrother : MonoBehaviour
{
public GameObject bigBrother;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Wall")
{
Instantiate(bigBrother);
bigBrother.SetActive(true);
}
}
}
Answer by endasil_unity · Apr 26, 2020 at 10:04 AM
Instantiate(bigBrother) creates a clone of the bigbrother object, then with bigBrother.SetActive(true); you activate whatever is your original bigBrother object, not the cloned object. Try this:
GameObject clone = Instantiate(bigBrother) as GameObject;
clone.SetActive(true);
Answer by JohnKMcCarthy · Apr 26, 2020 at 01:09 PM
Oh yeah that looks useful as it defines the name rather than let it be auto generated - that will be much more better for my intended use.
Answer by ransomink · Apr 26, 2020 at 02:11 PM
Is your bigBrother game object already in the scene? If it is, then you just need to do: bigBrother.SetActive(true);
when the object's collide
If you want to spawn your bigBrother object into the scene, when you instantiate, you do not save the created game object to a variable, so you cannot access it and set it to active. This will create a game object and set it to active. Instantiate also has parameters to set the position, rotation, and parent of the created game object. Add those, if needed
GameObject clone = Instantiate(bigBrother);
clone.SetActive(true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612232509im_/https://answers.unity.com/themes/thub/images/avi.jpg)