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 hanso23 · May 19, 2015 at 01:44 PM · instantiateclone

(solved)Instantiate prefab "on top" of previous one

Hey everyone! As a coding beginner, I'm stuck with this problem, and I bet you guys can help me out easily.

I have several Prefabs containing a plane with an animated textures on it. When I press the corresponding keyboard button, a clone gets instantiated ( f.ex. I press the spacebar -> prefab_1 gets instantiated). If I press another keyboard key, the newer instantiated prefab should be placed "on top" ( +1 on the z-axis) of the previous instantiated prefab and so on...

I do have managed to get the instantiation of the prefabs, but I'm stuck with the +1 on the z-axis / "on top" part. right now, they have a fixed "spawn" position.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class Ccontroller : MonoBehaviour {
 
 public Rigidbody ani01;
 public Rigidbody ani02;
 
 void Update() {
         if (Input.GetButtonDown("Fire1")) {
 
             Rigidbody clone;
             clone = Instantiate(ani01, new Vector3(0, 0, 3),Quaternion.identity) as Rigidbody;
 
 
 
         }
         if (Input.GetButtonDown("Fire2")) {
             Rigidbody clone;
             clone = Instantiate(ani02, new Vector3(0, 0, 3),Quaternion.identity) as Rigidbody;
             
         }
 
     
                 
     
     }
 
 }

additional info: It's an orthographic camera in the scene and all the prefabs have a script attached, which give them a tag and destroys older clones of the same prefab in the scene when the same button is pressed twice. so there is always just 1 clone of the same prefab in the scene.

If you guys could help me out, that would be super awesome!

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

Answer by Cherno · May 19, 2015 at 03:47 PM

Decalre an int variable in the main body, and increase it by one every time a prefab gets instantiated. Make the Instantiate function's position parameter use the int value as the Vector3's y value.

  using UnityEngine;
  using System.Collections;
  
  public class Ccontroller : MonoBehaviour {
  
  public Rigidbody ani01;
  public Rigidbody ani02;
  public int currentHeight = 0;
  
  void Update() {
          if (Input.GetButtonDown("Fire1")) {
  
              Rigidbody clone;
              clone = Instantiate(ani01, new Vector3(0, currentHeight, 3),Quaternion.identity) as Rigidbody;
              currentHeight ++;
  
  
  
          }
          if (Input.GetButtonDown("Fire2")) {
              Rigidbody clone;
              clone = Instantiate(ani02, new Vector3(0, currentHeight, 3),Quaternion.identity) as Rigidbody;
              currentHeight ++;
              
          }
  
      
                  
      
      }
  
  }
Comment
Add comment · Show 1 · 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 hanso23 · May 20, 2015 at 07:28 AM 0
Share

Hey Cherno! thanks for posting this! It's working prefectly! I just had to use another axis but all cool! Thanks man, made my day!!

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

Instantiate Reference Problem 1 Answer

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

Following Object can't find newly Instantiated Object (Solved) 1 Answer

Destroy a specific instantiated clone? 2 Answers

When cloning, how do you make your clones keep the original properties? 2 Answers


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