Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
avatar image
0
Question by Zornbock · Mar 23 at 10:32 PM · instantiateprefabpositionvectors

Script instantiation always same position

Hello guys,

i try to instantiate a Prefab at a certain position depending on how many instances already were instanciated. The project is in 2D.

 public GameObject projectObj;
 float y_pos = 0;
 
 public void NewButton()
     {
         y_pos += 10;
         GameObject inst = Instantiate(projectObj, new Vector3(100, y_pos, 0), Quaternion.identity);
     }

Goal: Instanciate iteratively by button pressing at different y positions = n*10. Problem: Instantiation always at (0, 0) of parent object. I read many threads but did not find a similar problem. Thanks for your help in advance :)

PS: tried to change from Vector3 to Vector2 and played around with datatypes but never changed behaviour: even using Vector3(1000,1000,0) or Vector2(1000,1000) delivering the same result so i must have a misunderstanding here.

Regards Chris

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 Rachmanichou · Mar 24 at 08:59 AM

Since nothing seemed wrong I plugged in the code into a test scene, and it worked out very well. How are you calling the method? I called it in the Update method and it worked just fine.

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 Zornbock · Mar 24 at 09:05 AM 0
Share

Hello and thank you fow your reply! I am calling the method by "On Click()" of a button. Additional info: The script is attached to a canvas which is holding the button (which i use for the call) as a child. The instances are created but they all overlap at (0, 0) and do not get instanced at the desired position: alt text

capture.png (69.0 kB)
avatar image Rachmanichou Zornbock · Mar 24 at 10:23 AM 0
Share

Hi,

The problem, imho, is that the onClick() method is a unity event, which are at the same time - to my understanding - events and the delegates that pass it to an event handler, which will produce the appropriate response. When using only, onClick(), you integrate the event but it is not properly handled. What I just said could be completely wrong, but that's the way I visualize it, until I'll some day find some solid information about Unity Events and Listeners. Anyways, what I suggest is simply adding a part in your that assigns a Listener to your unity event. Here's what I plugged into the code:

using UnityEngine.UI;

 public class test : MonoBehaviour
 {
     public GameObject projectObj;
     public Button button ;
     float y_pos = 0;
 
     void Start()
     {
         button.onClick.AddListener(DoOnClick) ;
     }
  
     public void NewButton()
     {
         y_pos += 10;
         GameObject inst = Instantiate(projectObj, new Vector3(100, y_pos, 0), Quaternion.identity);
     }
 
     void DoOnClick()
     {
         NewButton() ;
     }
 }

I placed that code on the Canvas, but it can be placed anywhere I think. And it did it for me! Hope that helps

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

210 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 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

How do I get the global position 2 Answers

How to instantly move a newly instantiated prefab? 2 Answers

How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

Prefab instantiating problem 1 Answer

Wrong particle and prefab spawn position 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