Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ciceri · Apr 13, 2016 at 07:29 PM · instantiatevector3rigidbody2dvector2

Duplicate an object if is at a position

I have to duplicate an 2D Rigidbody if it's at a specific position. How can i do it? I must use 2D Vector or 3D?

Thank to all in advance.

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 Highwalker · Apr 13, 2016 at 07:43 PM

 if (transform.position == targetPosition) {
 GameObject newObj = new this.gameObject;
 Instantiate (newObj, transform.position, Quaternion.identity);
 }

Alternatively, you can set it inside a trigger placed wherever your target position is.

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

Answer by Ciceri · Apr 15, 2016 at 06:04 PM

I use this code, but it doesn't work:

 using UnityEngine;
 using System.Collections;
 
 public class Duplicate : MonoBehaviour {
 
     public Rigidbody2D ballrigid;
 
     public GameObject ball;
 
     public Vector3 target;
     public Vector3 start;
     private Vector3 position;
 
     public Quaternion rotation;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         position = new Vector3 (0, transform.position.y, transform.position.z);
 
         if (Input.GetKeyDown(KeyCode.A)){
             Instantiate (ball, start, rotation);
         }
     
     }
 }
 

Comment
Add comment · Show 5 · 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 Highwalker · Apr 15, 2016 at 07:38 PM 0
Share

Try storing a new ball in a temp variable

 GameObject newObj = new ball;
  Instantiate (newObj, transform.position, Quaternion.identity);
avatar image Ciceri Highwalker · Apr 15, 2016 at 08:01 PM 0
Share

sorry the right code is : using UnityEngine; using System.Collections;

 public class Duplicate : $$anonymous$$onoBehaviour {
 
     public Rigidbody2D ballrigid;
 
     public GameObject ball;
 
     public Vector3 target;
     public Vector3 start;
     public Vector3 position;
 
     public Quaternion rotation;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         position = new Vector3 (0, 0, 0);
 
         if (ballrigid.transform.position.x == position.x){
             Instantiate (ball, start, rotation);
         }
     
     }
 }
 
avatar image Ciceri Highwalker · Apr 16, 2016 at 08:06 AM 0
Share

I'm sure that the problem isn't about the instantiate function, because if i try to put in the if statement (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) when i press "A" the object is duplicated in the correct position.

avatar image Highwalker Ciceri · Apr 16, 2016 at 08:29 AM 0
Share

Well the get$$anonymous$$eyDown method simply returns true or false. So anything that'll return true will trigger it the same. I'm guessing the problem is that you check if the balls x position is equal to zero. So if it's 0.001, it will still return false. You could check for a range, so something like

  if (transform.position.x > 0 - range && transform.position.x < 0 + range) {
     //Instantiate
     }

Or you could simply use a trigger. So create an empty object, and add a collider to it. Check isTrigger. And create a script for that object (the object with the trigger) with

   void OnTriggerEnter (Collider col) {
     //Instantiate
     }

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

63 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

Related Questions

Instantiate Object using Vector3 at posision? 1 Answer

My movement animation doesnt respect my scroll rect 0 Answers

instantiate keeps repeating itself 0 Answers

Turn a vector3 relative to a look direction 1 Answer

A line between two objects with conditions 0 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