• 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 JVILL · Jan 31, 2013 at 08:17 AM · gameobjectinstantiatetransformparentchild

Instantiate Terrain Object as child of Empty Game Object

I realise this has been asked many times already, but I've just come from FOUR other threads with similar titles, tried what they said, and couldn't get it to work.

I have a script attached to an empty Game Object (with box collider attached). I want to instantiate a terrain piece as a child of the object.

So far I have:

 var tile;
 
 tile = Instantiate (fHigh1, Vector3(r, 0, c), transform.rotation);
 
 tile.transform.parent = this;

A multitude of different errors pop up with every combination I try. I've also tried:

 tile.transform.parent = transform;
 tile.transform.parent = Transform;
 tile.Transform.parent = transform;
 tile.Transform.parent = Transform;
 tile.transform.this = transform;

Help please :(

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jan 31, 2013 at 08:26 AM

I would think your error is there:

 tile.transform.parent = this;

'this' represents the script not the object.

try

 tile.transform.parent = transform;

The code below works for me (in C#):

 public class Script : MonoBehaviour {
     public GameObject tile;
     void Start () {
         GameObject obj = (GameObject)Instantiate(tile,new Vector3(0,0,0),Quaternion.identity);
         obj.transform.parent =transform;
     }
 }


Note: Since you are using UnityScript, if you have #pragma strict at the top var tile; should be var tile:GameObject;

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 JVILL · Jan 31, 2013 at 08:48 AM 0
Share
 var tile :  GameObject ;
 tile = Instantiate (fHigh1, Vector3(r, 0, c), transform.rotation);

Throws error: "BCE0022: Cannot convert 'UnityEngine.Transform' to 'UnityEngine.GameObject'."

And as you read above, I already tried:

 tile.transform.parent = transform;

With no success. :( Perhaps it is in fact:

 tile.transform.parent = transform;

but I have to fix the other issue first?

avatar image JVILL · Jan 31, 2013 at 08:49 AM 0
Share

Also, I don't know C# yet. XD

avatar image fafase · Jan 31, 2013 at 10:23 AM 0
Share

You may have different type of variables, so to make it simple just make them all GameObject:

 var obj:GameObject;
 var tile:GameObject;
 function Start () {
     tile = Instantiate(obj,Vector3(0,0,0),Quaternion.identity);
     tile.transform.parent =transform;
 }
avatar image JVILL · Jan 31, 2013 at 10:58 AM 0
Share

I did what you said and played:

InvalidCastException: Cannot cast from source type to destination type. tileCollider.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/tileCollider.js:57)

:(

avatar image JVILL · Jan 31, 2013 at 11:01 AM 0
Share

A preview:

var tile : GameObject; var fHigh1 : GameObject;

 function OnTriggerEnter ( other : Collider )
 {
 r = other.transform.position.x;
 c = other.transform.position.z;

 
 if ( other.gameObject.name == "fHigh1")        
 {
 tile = Instantiate (fHigh1, Vector3(r, 0, c), Quaternion.identity);
 }
 
 tile.transform.parent = transform;
 
 }

Note the other.gameObject.name is referring to a DIFFERENT "fHigh1" object.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

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

Getting instance of an sub object rather than the original's subobject 0 Answers

Instantiating a new gameObject as a child of a different gameObject 2 Answers

Instantiate prefab as Child to remove and re-do later 2 Answers

iOS Instantiate transform as child -- positioning bug 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges