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 /
avatar image
0
Question by $$anonymous$$ · Nov 21, 2018 at 11:17 AM · prefabbuttonpositioninstanceonclick

Script uses transform.position of Prefab instead of Instance

The script is attached to a prefab ("build space").
I instantiate a number of them at different positions (from a different Script). That works as intended, I get all the instances where I want.
Clicking on one of the Instances opens a UI Menu:

     private void OnMouseDown() {
         if (!EventSystem.current.IsPointerOverGameObject()) {
             Instantiate(buildMenuCanvas, transform.position, transform.rotation);
             Debug.Log("BuildSpace at Position " + transform.position);
         }     
     }

This works as intended, and the transform.position is also what I expect (i.e. of the instance that was clicked).

Clicking a button on the UI calls the Build function, which should do 3 things:
- close the UI (works)
- Destroy the "build space" Instance that was clicked on
- Instantiate a "building" at the position of the "build space"

     public void Build() {
         Instantiate(building, transform.position, transform.rotation);
         Debug.Log("Instantiated Building at " + transform.position);
         Destroy(GameObject.FindGameObjectWithTag("UIPopup"));
         Destroy(this.gameObject);
     }

The "buildings" get instantiated, but not at the position of the "build space" that was clicked, but at the position of the "build space" Prefab. Also, the "build space" Instance is not destroyed.

It seems the Build function runs on the prefab instead of the instance. (I also get a 'destroying assets is not permitted' error). As I said above, in OnMouseDown, the position is correct. Could it be that it is because the referenced Object for the Buttons OnClick is the Prefab?
How can I make sure that the Build function is called on the Instance that was clicked on?

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 gewl · Nov 21, 2018 at 04:23 PM

You got it! This is because the referenced Object for the Button's OnClick is the prefab.

In order for the Button to dynamically call the Build method on the correct "build space" prefab, you need to dynamically assign the OnClick listener at runtime. The precise way to do this is going to differ a bit depending on how you've implemented things. Also, unrelatedly, I really recommend having one "buildMenuCanvas" that you turn on/off and move around the screen instead of creating/deleting a new one every time you open the menu—creation/deletion is expensive!

So the gist is going to be:

  1. Give your UI menu controller a public ActivateBuildMenu method, that takes in the BuildSpace script. In this ActivateBuildMenu method:
    • turn the build menu's gameObject on,

    • change its position based on the screenspace of the build space instance, and

    • assign an onClick listener to the Button that calls the Build method on the passed BuildSpace script (this will tell you how to assign listeners at runtime). Also, have this listener turn the UI menu controller off, so that your BuildSpace doesn't need to be responsible for the state of UI elements!

  2. In your UI menu controller's OnDisable, have it clear the button of all listeners.

  3. Change your BuildSpace script to have a serialized reference to your UI menu controller, and have it call ActivateBuildMenu(this) instead of Instantiating the menu object.

Again, the precise implementation can be moved around a bit, but that's the gist of it.

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 $$anonymous$$ · Nov 21, 2018 at 06:22 PM 0
Share

Thank you. That should be enough to get me what I want.

I really recommend having one "build$$anonymous$$enuCanvas" that you turn on/off and move around the screen ins$$anonymous$$d of creating/deleting a new one every time you open the menu—creation/deletion is expensive!

I assumed as much. I'm rather new to Unity, so I'm often just brute-forcing things as I learn, but thanks for the additional advice.

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

145 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

Related Questions

How do I get the global position 2 Answers

My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer

Enemy only chasing pre-fab's original location, not instance's 2 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

Weird behaviour when trying to dynamically create buttons 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