Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Mcboss762 · Dec 15, 2018 at 12:50 AM · buttonscripting beginnerscriptingbasicsbuttonsbutton trigger events

Button make object teleport (simple)

Hi, I have a script that makes an object teleport to random locations in my world:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Teleport : MonoBehaviour {
 
     public Vector3 offset;
     public Transform player;
 
     void Start() {
         float randomY = Random.Range(-10.0f, 10.0f); // Min = -10.0f, Max = 10.0f
         float randomX = Random.Range(-10.0f, 10.0f);
         offset += new Vector3(randomX, randomY, 0);
     }
 
      void Update() {
         Vector3 position = transform.position;
         position.y = (player.position + offset).y;
         position.x = (player.position + offset).x;
         transform.position = position;
     }
 
 }

And when I start the game, it teleports to a random spot, however, I would like to have a button run the script to make it teleport somewhere alt text

How would I do this? Thanks so much for anyone who helps :)

tl;dr: I have a script that on start, makes something teleport, but i want a button to do it instead.

screen-shot-2018-12-15-at-114400-am.png (16.5 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 Vega4Life · Dec 15, 2018 at 01:20 AM 0
Share

A UI button or a hotkey?

avatar image Mcboss762 Vega4Life · Dec 15, 2018 at 01:27 AM 0
Share

UI button.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Dec 15, 2018 at 02:11 AM

Here is the code. Just moving your Start() contents into a DoTeleport() function so our button can call it when clicked.


 using UnityEngine;
 
 public class Teleport : MonoBehaviour
 {
     public Vector3 offset;
     public Transform player;
 
 
     void Update()
     {
         Vector3 position = transform.position;
         position.y = (player.position + offset).y;
         position.x = (player.position + offset).x;
         transform.position = position;
     }
 
     // Assign this to your UI buttons onClick
     public void DoTeleport()
     {
         float randomY = Random.Range(-10.0f, 10.0f); // Min = -10.0f, Max = 10.0f
         float randomX = Random.Range(-10.0f, 10.0f);
         offset += new Vector3(randomX, randomY, 0);
     }
 }


Next, you want to create a UI button. A the top of the editor click on GameObject -> UI -> Button. This will add a Canvas with a UI button in it. There is a button script on the button object. At the bottom hit the + sign to add an OnClick() event. Drag the gameObject that is holding your Teleport script into the reference. Next to it, it should say "No Function". Click on this button and it opens a drop down. Hover over our Script 'Teleport', then hover over our function 'Do Teleport' and click it. Now we are all set.


Clicking the button will now send an event to that function that will execute your code.


Hope that helps!


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

184 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

Related Questions

How to make an object keep move when pressing a button? 0 Answers

How to make button rotate when another button is pressed? 0 Answers

UI Button OnClick Only Works the First Time 2 Answers

Unity Broken Buttons 0 Answers

Does the sequence in which I keep the objects in a scene matter really? 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