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 /
  • Help Room /
avatar image
0
Question by kallied · Feb 22, 2017 at 01:23 PM · transformbutton

Button Transforms Player position

Hi I have a few on-screen buttons that will transform a player position. I can get it to work using KeyCode but I'm trying to use GetButton so i can use the buttons instead. To achieve this and have created a string for the button Name. I am getting an error saying the buttonNameC does not exist in this context. I would appreciate any help. Thanks

using UnityEngine; using System.Collections;

public class TeleportOnScreen : MonoBehaviour { private GameObject Position1;

 string buttonName = buttonNameC();

 private void Start() 
 {
     Position1 = GameObject.Find ("Position1");
     Position2 = GameObject.Find ("Position2");
     Position3 = GameObject.Find ("Position3");
 }
 private void Update() 
 {
     if (Input.GetButton(buttonNameC))
     {
         transform.position = Position1.transform.position;
     }

     if (Input.GetKey(KeyCode.A))
     {
         transform.position = Position2.transform.position;
     }

 }


}

Comment
Add comment · Show 4
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 Hellium · Feb 22, 2017 at 02:24 PM 0
Share

What do you mean by "Button" ? Buttons on the GUI system of Unity ?

avatar image kallied Hellium · Feb 25, 2017 at 04:58 AM 0
Share

Correct, Buttons on GUI system of Unity

avatar image kallied kallied · Feb 25, 2017 at 05:05 AM 0
Share

I encapsulated with "" as suggested by letitslide which gets rid of the error, excellent. However i'm missing something major. I've added the script to the button - all good. I've have associated the Button Object to the Unity GUI Button which i think is wrong because i have no function to call. I have rewritten my code and associated to a Button object which transform my button position. This makes sense but not what i want. I really need to tranform the position of my FP camera.

avatar image kallied Hellium · Feb 25, 2017 at 05:21 AM 0
Share

I think I'm getting somewhere. I need to associate the script to the FPSController. The issue is still linking the Button with the script. On the Button inspector i need to call a function relating to buttonNameC which will move my FP position. How do i link my Button with the string ButtonNameC?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Feb 25, 2017 at 09:00 AM

Here is a script which works ;)

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 [System.Serializable]
 public class TeleportButton
 {
     public Button Button ;
     public Transform DestinationPoint;
     
     public void AddListener( Transform t )
     {
         Button.onClick.AddListener( () => t.position = DestinationPoint.position );
     }
 }
 
 public class TeleportOnScreen : MonoBehaviour
 {
      // Unfold the TeleportButtons 'folder' in the inspector and choose a size (3 for example)
     // Then, drag & drop your spawn points as Transform and your buttons from the canvas
     public TeleportButton[] TeleportButtons ;
 
     private void Start() 
     {
         Transform selfTransform = GetComponent<Transform>();
         
         for( int i = 0 ; i < TeleportButtons.Length ; ++i )
         {
             TeleportButtons[i].AddListener(selfTransform) ;
         }
     }
     
     // Not necessary, but usefull to see where your spawn points are located
     private void OnDrawGizmos()
     {        
         for( int i = 0 ; i < TeleportButtons.Length ; ++i )
         {
             Transform t = TeleportButtons[i].DestinationPoint ;
             
             if( t == null )
                 continue ;
             
             Gizmos.color = Color.red;
             Gizmos.DrawLine(t.position, t.position + t.right);
             
             Gizmos.color = Color.blue;
             Gizmos.DrawLine(t.position, t.position + t.forward);
             
             Gizmos.color = Color.green;
             Gizmos.DrawLine(t.position, t.position + t.up);
         }
     }
 }
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 kallied · Feb 25, 2017 at 11:10 AM 0
Share

Thanks so much. I'll have ago. Really appreciate it.

avatar image
0

Answer by AMU4u · Feb 23, 2017 at 12:48 AM

I haven't tested, but you probably need to encapsulate with "" when you try calling it in update.

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

111 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

Related Questions

(26,47): error CS1525: Unexpected symbol `)' 1 Answer

Button Displacement 0 Answers

Move gameObject on UI Button Press 2 Answers

Add Force isnt working 1 Answer

Moving UI buttons with code 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