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;
}
}
}
What do you mean by "Button" ? Buttons on the GUI system of Unity ?
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.
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?
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);
}
}
}
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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