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
1
Question by epicpython · Jul 16, 2017 at 06:16 PM · buttonruntimeprefab changing at runtime

How can I instantiate a button prefab, complete with onClick() method, at runtime?

Background: I am making a tower defense game, and I want to be able to switch out the buttons for each level, without making a new scene for each level. Basically, I want to be able to say, in code, "When you load this level, in addition to setting up the enemy spawning, create 4 buttons that the player can click to place towers", and have me, the programmer be able to set those 4 towers to different ones for different levels.

So I need button prefabs for all of them, and I need to instantiate them at runtime, and get them to each run a method (see code) that has unique arguments (tower type, cost) when clicked.

Here's my code so far:
public class BuyDragable : MonoBehaviour {

 public GameObject drag;
 public int cost;
 // Use this for initialization
 public void BuyStuff()
 {
     if (GameObject.FindObjectOfType<ScoreManager> ().SpendDna (cost)) 
     {//checks to see if the player has enough money to buy it
         Instantiate(drag, this.transform.position, this.transform.rotation);
     }
 }

}

This script is attached to my button, and is set to run OnClick() (see picture.) What I want to do is create several prefabs which I can instantiate, which have the same script (BuyDragable), but have different parameters (the cost of the tower, and the tower object itself). The different buttons also need to have different pictures.

So how is the best way to do this? Can I instantiate these prefabs through code, and how? Would it be easier to place the button prefabs in the scene before runtime, and just set the button image, tower object, and cost of tower at runtime? How would I do that?

Thanks!alt text

buttonproblems.png (213.9 kB)
Comment
Add comment · Show 2
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 Cherno · Jul 17, 2017 at 12:29 AM 0
Share

Use AddListener:

 myButton.onClick.AddListener(delegate{YourFunction(Parameter);});

to assign an OnClick event.

Remember to use RemoveAllListeners when you add a new one if you don't want multiple events.

avatar image Marc_Keil · Mar 16, 2018 at 05:12 PM 0
Share

Could anyone solve the question?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cynikal · Jul 16, 2017 at 07:25 PM

You could always do it the old fashioned way and create a script where when it's clicked on that it triggers the command you want.

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 epicpython · Jul 17, 2017 at 12:18 AM 0
Share

But how do I give the script different values at runtime? For example, sometimes calling the script and sending it one tower prefab and int cost, and other times calling it with another tower prefab and a different cost?

Because the only way I know how to change the values that the script takes is through the inspector, and I obviously can't use that to change them at runtime.

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

78 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

Related Questions

How can I dynamically create scene ? 1 Answer

Prefabs instantiated at runtime disappear right after 0 Answers

Runtime writing text. 2 Answers

Ui Button: call function "on pointer enter", all at runtime 1 Answer

How to edit prefab instances at Runtime/Creation Time 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