- Home /
Instantiate prefab as Child to remove and re-do later
Ok I have been at this problem for ages, and everywhere I find is saying the same:
GameObject theRoom;
//code
theRoom = Instantiate(Resources.Load("MyPrefab")) as GameObject;
//set as child
theRoom.transform.parent = transform;
Even though everywhere suggests it; I get the following Error:
Assets/Standard Assets/Scripts/MY Scripts/RoomManager.cs(22,33): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
If I remove the "as GameObject" part and just assign the Instantiate(...) to the variable, the I get the following Error:
Assets/Standard Assets/Scripts/MY Scripts/Room.cs(35,17): error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?)
Transform if the variable is a Transform, and same error if I replace it as a GameObject.
The Idea of the program, is in a editor like setup, the player calls a method on room manager, which depending on the players position access the Room in it's array (which have pos and model) and on this would call the method to destroy the old child, and add a new one; effectively changing the room.
Here is some code:
using UnityEngine;
using System.Collections;
public class Room : MonoBehaviour {
private GameObject roomObj;
private int posX = 0, posY = 0, posZ = 0;
Vector3 pos;
private GameObject theRoom;
public Room(GameObject rO, int x, int y, int z){
this.roomObj = rO;
this.pos.x = x;
this.pos.y = y;
this.pos.z = z;
}//
//Use this for initialization
void Start () {
theRoom = ( Instantiate( roomObj, pos, Quaternion.identity ) )as GameObject;
theRoom.transform.parent = transform;
}//Start
//------------------------------------------
// Update is called once per frame
void Update () {
}//Update
//------------------------------------------
//
void ChangeRoom( GameObject newRoom ){
//destroy old child
//instantiate newRoom
}//GiveRoom
}//CLASS
Thanks for any help is welcome, annoying how any mention like this I find suggests what just isn't working for mine.
Answer by VioKyma · Oct 05, 2013 at 10:38 AM
You have not initialised the vector.
Try
public Room(GameObject rO, int x, int y, int z){
this.roomObj = rO;
this.pos = new Vector3();
this.pos.x = x;
this.pos.y = y;
this.pos.z = z;
}
or even
this.pos = new Vector3(x, y ,z);
God how did I forget to initialise the vector for position, ok im rewriting the ints to floats as Vector3 requires floats.
I may comment again if I still have an error, but will look more closely, sorry for the idiocy; only second week back to Uni, still getting back into coding groove.
Vector3's cant use ints though? only floats, so I am currently changing the code to pass in floats.
That's what I was trying to tell you in the other comments. :)
It's cool, happens to everyone. Also, watch out for writing constructors for a $$anonymous$$onoBehaviour. It can get you into trouble if you're not careful. Let me know if this fixes the error, or if you're still having trouble.
Late night coding is bad :D
Best to remember these now rather than later, thanks for the help gonna try and sort this out.
$$anonymous$$ay try using this.pos = new Vector3( (float)x, (float)y, (float)z );
Will do and thanks both of you.
Additionaly, what do you mean by a constructor for a $$anonymous$$B?
All I want to do is write some code to spawn some PreFab-Emptys which will then have a model spawn via changeRoom.
The idea is Room$$anonymous$$anager creates the Rooms, and when their Start runs, they instantly create the Empty Room and from there any new model as a child which can be destroyed and then remade with a new model.
Answer by mattssonon · Oct 05, 2013 at 09:32 AM
Try doing theRoom = (GameObject)Instantiate(Resources.Load("MyPrefab"));
That does not work, still have the same error, pointing to line 22 of Room$$anonymous$$anager.
using UnityEngine;
using System.Collections;
public class Room : $$anonymous$$onoBehaviour {
private GameObject roomObj;
private int posX = 0, posY = 0, posZ = 0;
Vector3 pos;
private GameObject theRoom;
public Room(GameObject rO, int x, int y, int z){
this.roomObj = rO;
this.pos.x = x;
this.pos.y = y;
this.pos.z = z;
//line22
//Pretty sure I dont need one for every "variable"
}//
Assets/Standard Assets/Scripts/$$anonymous$$Y Scripts/Room$$anonymous$$anager.cs(22,33): error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected
this.pos.z
is not an int, it's a float. The code you're showing me is not from Room$$anonymous$$anager.cs, it's from Room, at least that's what the class is called. Could you show me the code where you initialize the Room object?
Certainly, the x,y,z being taken in are ints, not floats.
Current Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Room$$anonymous$$anager : $$anonymous$$onoBehaviour {
public static int roomsX = 3, roomsZ = 3;
//non-static presents error #1
public List<GameObject> Room$$anonymous$$odels = new List<GameObject>();
private Room[,] Rooms = new Room[roomsX,roomsZ];
// Use this for initialization
void Start () {
//moving roomX/Z intializing to here presents **error #2**
for(int x=0; x<roomsX; x++){
for(int z=0; z<roomsZ; z++){
Room[x,z] = new Room( Room$$anonymous$$odels[0], x, 0, z );
}//for
}//for
}//Start
Current Error & Error #2
../Room$$anonymous$$anager.cs(25,33): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Error #1:
../Room$$anonymous$$anager.cs(15,42): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Room$$anonymous$$anager.roomsX' (additional error for roomsZ)
public class Room : $$anonymous$$onoBehaviour {
private GameObject roomObj;
private int posX = 0, posY = 0, posZ = 0;
Vector3 pos;
public GameObject theRoom;
public Room(GameObject rO, int x, int y, int z){
roomObj = rO;
pos.x = x;
pos.y = y;
pos.z = z;
//Pretty sure I dont need one for every "variable"
}//
Thanks for the help.
As per http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html the x, y, and z components of a Vector3 are floats, not ints.
Do not initialize Room[,] Rooms
outside of Start(), just declare it and initialize it inside Start(), i.e. Rooms = new Room[roomsX,roomsZ];
. And then make roomsX and roomsY non-static, what errors are you getting then?
Thanks for the Help and Advice guys, if you happen to get an update of this comment or pass by, heres the video I mentioned I might link: http://www.youtube.com/watch?v=XIAs99-UOPo
Or head to my channel Yin117 to view it.