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 NZMGames · May 22, 2017 at 03:50 AM · buttons

Button to summon prefab

I need a button that I created on a canvas to summon one of my prefabs on screen. I'm doing a tower defense game in C# and I have no idea what I;m doing....i need this answered in like 2 or 3 days..

Comment
Add comment · Show 1
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 RobAnthem · May 22, 2017 at 03:56 AM 0
Share

This isn't really a question, you are kind of demanding a script. That's not what Q&A is for... Also, you have to understand, the moderation queue becomes massive very quickly, we're 3 days behind on all questions at the moment, and we do our best to keep things moving.

I was going to reject this, but I can help you out a little... Still you need to actually learn C# and Unity and ask questions that pertain to problems, or learning, not a demand for a script...

Attach this to your UI button, and drag and drop the spawn location object into the Spawn Location slot of the script, and the Prefab you want to spawn into the Prefab slot.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class SpawnPrefab : $$anonymous$$onoBehaviour
 {
     public GameObject prefab;
     public GameObject spawnLocation;
     void Awake()
     {
         Button button = GetComponent<Button>();
         if (button)
         {
             button.onClick.AddListener(Spawn);
         }
     }
     void Spawn()
     {
         Instantiate(prefab, spawnLocation.transform.position, spawnLocation.transform.rotation);
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Skaster87 · May 24, 2017 at 07:41 AM

Hi,

You need to add an on click event to the button that runs your instantiate function.

So you'll create the button in the canvas, and in the inspector find the On Click () box and press the + at the bottom. Add the game object that contains the script with the function, then select the script component and function from the drop down menu and it should work.

So you'll need to create a GameObject with a script attached that contains a call to instantiate or reuse the object from the pool. Let me know if you need specific examples.

Brackeys does a great Tower Defense tutorial that covers this topic. Specifically episodes 12 and 17 https://m.youtube.com/playlist?list=PLPV2KyIb3jR4u5jX8za5iU1cqnQPmbzG0

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make buttons that turn into a text field? 2 Answers

assign keyboard buttons 1 Answer

Problem making buttons from array of array 1 Answer

Random Button / Plane Generation. 0 Answers

Using NGUI how to get button focused event 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