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 RoxTeddy · Jul 13, 2014 at 03:13 PM · 2dinstantiateprefabschildclone

Prefab clones behaviour

Hello everyone,

I don't get how prefab instantiation works. I'm coding a 2d vertical infinite scrolling and I made a script that continually create new tiles of background (prefab) at top and destroy itself when its at bottom. Then each background instantiate a group of windows (prefab too). Later some window would be animated or scripted or whatever so it is important that each window is a different gameObject.

The problem is, each background seems to herit windows from the last background created. So 1st bacnkground has 3 windows, 2nd has 6, 3rd has 9... etc..

alt text

like if all child were cloned too... but they dont exist in the prefab, I create them at runtime..

I'm sure I'm not using those prefabs the good way but that is my first Unity Project I don't how to handle the all-situation so if you have any clue..

here's my code :

 using UnityEngine;
 using System.Collections;
 
 public class bg_parallax : MonoBehaviour
 {
     static public Vector2         speed = new Vector2 (5, 5);
     static public Vector2         direction = new Vector2 (0, -1);
     public GameObject     background;
     public GameObject     window;
     private bool        needChild = true;
     // Use this for initialization
     void Start ()
     {
         ((GameObject)Instantiate (window, new Vector3 (-1.8f, -1.5f, 19f), transform.rotation)).transform.parent = transform;
         ((GameObject)Instantiate (window, new Vector3 (0f, -1.5f, 19f), transform.rotation)).transform.parent = transform;
         ((GameObject)Instantiate (window, new Vector3 (1.8f, -1.5f, 19f), transform.rotation)).transform.parent = transform;
     }
 
     // Update is called once per frame
     void Update ()
     {
     }
 
     void FixedUpdate ()
     {
         transform.Translate (new Vector3 (speed.x * direction.x, speed.y * direction.y, 0) * Time.deltaTime);
         if (needChild && transform.position.y <= 0) {
             ((GameObject)Instantiate (background, new Vector3 (0, transform.position.y + renderer.bounds.size.y, 20), transform.rotation)).transform.parent = transform.parent;
             needChild = false;
         }
         if (transform.position.y <= renderer.bounds.size.y * -1)
         {
             for(int i = 0; i < transform.childCount; i++)
                 DestroyImmediate(transform.GetChild(i).gameObject);
             Destroy (gameObject);
         }
     }
 }


screen shot 2014-07-13 at 15.28.37.png (10.0 kB)
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 Kiwasi · Jul 13, 2014 at 07:40 PM

The clue is the (clone)(clone)

You are calling instantiate on the existing GameObject, not the prefab. Each time you clone a GameObject it clones all of its children as well. Make sure you have the prefab assigned from the folder structure. Not an instance of the prefab from the hierarchy.

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 RoxTeddy · Jul 13, 2014 at 09:31 PM 0
Share

Right. That is the reason thank you. The problem is when i point the prefab in the inspector, it links to the active gameObject, not the prefab.. I'm looking for another way to instantiate the prefab.

avatar image tanoshimi · Jul 13, 2014 at 09:35 PM 1
Share

Drag the prefab from the project window onto the background slot, not from the scene view.

avatar image RoxTeddy · Jul 13, 2014 at 10:00 PM 0
Share

Thank you Bored$$anonymous$$ormon. That 's why.

Now the problem is when I point to the prefab in the inspector it links automatically with the gameObject.. I dont know how to initialize it to use virgin prefab.

avatar image RoxTeddy · Jul 14, 2014 at 04:26 PM 0
Share

@tanoshimi : Thats basically what I do.

I Even tried to change object and prefab names. But even if I pick the prefab, It finally links to the gameobject.

So now I use a Ressources.Load(), it seems to work..

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiated GameObject's components are disabled 3 Answers

How can I make a game object follow an instantiated game object? 1 Answer

How can I instantiate new prefabs on top of older ones. 1 Answer

Prefab - Child of GameObject. 0 Answers

Changing a variable on an instantiated prefab clone once created 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