Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Yinoguns · Oct 04, 2013 at 10:54 PM · gameobjectinstantiateparentchild

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yinoguns · Oct 05, 2013 at 10:43 AM 0
Share

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.

avatar image Yinoguns · Oct 05, 2013 at 10:47 AM 0
Share

Vector3's cant use ints though? only floats, so I am currently changing the code to pass in floats.

avatar image mattssonon · Oct 05, 2013 at 10:51 AM 0
Share

That's what I was trying to tell you in the other comments. :)

avatar image VioKyma · Oct 05, 2013 at 10:55 AM 0
Share

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.

avatar image Yinoguns · Oct 05, 2013 at 10:55 AM 0
Share

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.

Show more comments
avatar image
0

Answer by mattssonon · Oct 05, 2013 at 09:32 AM

Try doing theRoom = (GameObject)Instantiate(Resources.Load("MyPrefab"));

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yinoguns · Oct 05, 2013 at 09:56 AM 0
Share

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

avatar image mattssonon · Oct 05, 2013 at 10:24 AM 0
Share

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?

avatar image Yinoguns · Oct 05, 2013 at 10:36 AM 0
Share

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.

avatar image mattssonon · Oct 05, 2013 at 10:45 AM 0
Share

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?

avatar image Yinoguns · Oct 06, 2013 at 06:32 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated GameObject collision without script repetition? 1 Answer

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Make a simple tree 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Instantiate GameObject Parent targetting issue. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges