Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by unity_DKZaovdDe7eF6A · May 19, 2021 at 03:20 AM · child objectparent-child

how to add prefab to gameobject?

I'm trying to have 'foundation' as a child of 'empty' but with my code it doesn't do that but it also gives no errors, what am I doing wrong?

 public List<GameObject> foundations = new List<GameObject>();
 public List<GameObject> levels = new List<GameObject>();
 public List<GameObject> roofs = new List<GameObject>();

 public BuildSystem buildSystem;

 GameObject empty;
 
 private int x = 0;


 public void CreateNewBuilding()
 {
     x++;
     empty = new GameObject("Building" + x);
 }

 public void StopBuilding()
 {
     Destroy(empty);
     x--;
 }

 public void AddFoundation(GameObject foundation)
 {
     foundations.Add(foundation);
     buildSystem.NewBuild(foundation);
     foundation.transform.SetParent(empty.transform);
 }
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

Answer by cotevelino · May 19, 2021 at 03:50 AM

Try to use transform.SetParent(wantedParent), store the new foundation that was instanced and after spawn assign the parent to it. hope it helps.

docs: https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

Comment
Add comment · Show 2 · 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 unity_DKZaovdDe7eF6A · May 19, 2021 at 11:15 AM 0
Share

I don't understand what you are saying. Right now I'm using foundation.transform.SetParent(empty.transform) if I use foundation.transform.SetParent(empty) there is an error because empty is a gameobject.

avatar image cotevelino unity_DKZaovdDe7eF6A · May 19, 2021 at 03:51 PM 0
Share

Well then idk why it doesn't work. Maybe u forgot somth in code or forgot to set references

avatar image
0

Answer by Owen-Reynolds · May 19, 2021 at 07:54 PM

A comment -- instead of new GameObject it's almost always better to Instantiate a prefeb. Even for an empty you probably want it set to a different layer, and want a script on it at (at least a data-only script). You can do that in code, but a prefab makes it easier.

 

As far as setting a parent, any way works. If G is a gameObject and Empty is another, then G.transform.parent=Empty.transform; is fine or G.transform.SetParent(Empty.transform); or when you Instantiate G use the option to set a parent. I've used them all and they're the same. If there's a problem it's something else. Look at the errors -- is it saying something is null? Maybe foundation isn't being created?
Comment
Add comment · 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

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

119 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lock rotation of child's specific axis? 0 Answers

Child Object won't move with parent 0 Answers

Foreach loop not going through all elements 1 Answer

How can the pivot point of a object be changed during Runtime? 0 Answers

Following object (arrow) slides off of a object 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