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 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.

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

Instantiate a Prefab as child 0 Answers

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

Instantiated GameObject collision without script repetition? 1 Answer


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