• 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 DubSket · May 03, 2014 at 07:49 PM · instantiatechild objectparent transform

Make instantiated prefabs the children of another gameObject

Hi guys,

I've seen a few similar questions to this around here but none of them solved my problem. I'm instantiating a series of items that I'd like to attach as children to another game object. The purpose of this is to affect the rotation of the parent object's transform.

 GameObject parentObj = Instantiate(items, new Vector3 (spawnPos.x + i*dist, spawnPos.y + j*dist, spawnPos.z + k*dist), Quaternion.identity) as GameObject;
   
 parentObj.transform.Rotate(Vector3.right * Time.deltaTime);

However, I'm getting an error saying parentObj onflicts with a declaration in a child block.

Is anyone able to point out what I'm doing wrong? It would be really appreciated.

Thanks.

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
1
Best Answer

Answer by Jeff-Kesselman · May 03, 2014 at 07:55 PM

It means you have parentObj declared twice in your method. Probably once at the method level and again within the body of an if or other sub-block.

Id need to see the entire method to tell you exactly where the conflict is.

Comment
Add comment · Show 4 · 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 DubSket · May 03, 2014 at 09:17 PM 0
Share

Hi, thanks for the reply. And you're right, this was the problem.

There is another error I'm getting to do with the movement of the the parent object's transform. Or rather it's lack of movement. I think it would be best if I showed you the whole code:

 using UnityEngine;
 using System.Collections;
 
 public class itemSpawn : $$anonymous$$onoBehaviour {
     
     private GameObject parentObj;
     public GameObject items;
     public Vector3 spawnPos = new Vector3(5,5,5);
     private int cLine$$anonymous$$ax = 2; //max spawn int
     private float dist = 1.5f;
     
     // Use this for initialization
     void Start () {
         
         //parentObj.AddComponent<Rigidbody>();
         
         for(int i=0; i<cLine$$anonymous$$ax; i++)
         {
             for(int j=0; j<cLine$$anonymous$$ax; j++)
             {
                 for(int k=0; k<cLine$$anonymous$$ax; k++)
                 {
                     
                     GameObject parentObj = Instantiate(items, new Vector3 (spawnPos.x + i*dist, spawnPos.y + j*dist, spawnPos.z + k*dist), Quaternion.identity) as GameObject;
                     //Instantiates the items into a parent gameobject
 
                 }
             }
         }
         
         //
         
     }
     
 
     void FixedUpdate () {
         parentObj.transform.Rotate(Vector3.right * Time.deltaTime);
     }
 }    

The items just do nothing. I really appreciated the help so far, do you have any ideas what the problem could be from the code?

avatar image DubSket · May 03, 2014 at 09:21 PM 0
Share

Additional note: There items are spawned into a giant cube of individual objects. I'm trying to get this to rotate constantly. Thought it might help to have some context.

avatar image Jeff-Kesselman · May 03, 2014 at 10:03 PM 1
Share

You are declaring parentObject as a local in your loop, so the global never gets set. Get rid of the declaration in the loop and just set it.

This:

 GameObject parentObj = Instantiate(items, new Vector3 (spawnPos.x + i*dist, spawnPos.y + j*dist, spawnPos.z + k*dist), Quaternion.identity) as GameObject;

Should just be:

 parentObj = Instantiate(items, new Vector3 (spawnPos.x + i*dist, spawnPos.y + j*dist, spawnPos.z + k*dist), Quaternion.identity) as GameObject;
avatar image DubSket · May 03, 2014 at 11:52 PM 0
Share

Thanks for the reply! This worked for me. It's led to a newer problem, but I think I can tackle this one myself.

$$anonymous$$uch appreciated!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Picking Up Items (Inventory Implementation Problem) 0 Answers

Instantiate On Container Child 1 Answer

Extremely confused with parent and child object system 1 Answer

Checking if object intersects? 1 Answer

Make child's transform independent of parent 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