- Home /
Refrencing Instantiated Object
Okay let me find the right words to my question.... ... .. . Okay I got it! How would I replace a single object that is being instantiated whilst not replacing all of the Instantiated objects, so a random selection of sorts? I'm having trouble doing this within my scripts here is what i want to replace but I only want to replace one of these not all of them. and I'm referring to the bool hasroom's Instantiated object closedroom. I'm replacing one of these rooms with an item shop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomPos : MonoBehaviour {
public float waitTime;
public LayerMask whatIsRoom;
bool hasRoom;
public GameObject closedRoom;
void Update () {
Collider2D room = Physics2D.OverlapCircle(transform.position, 1, whatIsRoom);
if (room != null) {
hasRoom = true;
}
if (waitTime <= 0)
{
if (hasRoom == false) {
Instantiate(closedRoom, transform.position, Quaternion.identity);
}
}
else {
waitTime -= Time.deltaTime;
}
}
}
Answer by TomTobby · Nov 18, 2019 at 06:48 PM
GameObject.Instantiate return the object that has being Instantiated, I believe if you store that object you can change the single object. I haven't test anything yet so, I hope I helped, and sorry for my English, I am more an Spanish speaker
edit: It would be something like
Object singleObj = Instantiate(gameobj, pos, rot);
edit: But you have to convert it to GameObject because it returns a UnityEngine.Object not a UnityEngine.GameObject
GameObject singleObj = Instantiate(gameobj, pos, rot) as GameObject;
singleObj.active=false;
His answer except I would add as GameObject
GameObject singleObj = Instantiate(gameobj, pos, rot) as GameObject;
singleObj.active=false;
I've found if i don't add that I run into issues
true, I haven't touched unity since like 6 months, I'll update my answer