Move UI Canvas position on right click
I have a prefab in Assets/Prefabs/InGameUI.prefab which is a UI Canvas with components inside. I want to make a script which would be able to change the position of the Canvas all together with right clicking. Here is what I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ingameuitransition : MonoBehaviour
{
GameObject prefab;
bool open;
private Vector3 openPosition;
// Start is called before the first frame update
void Start()
{
prefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/InGameUI.prefab", typeof(GameObject));
open = false;
openPosition = new Vector3(1000, 1000, 0);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1) && open == false)
{
open = true;
Debug.Log("yeah");
prefab.transform.position = openPosition;
}
}
}
However, when I test, nothing changes. Help!
hum why change position (throwing it off the view) when you can disable it? also you seem to forgot instantiate the prefab... try this
// keep the other variables, please add:
GameObject Instance; //instance of the prefab on scene
void Start()
{
prefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/InGameUI.prefab", typeof(GameObject));
Instance = Instantiate(Prefab);
open = false;
openPosition = new Vector3(1000, 1000, 0);
}
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(1) && )
{
//changed to return at next right click
if (!open){ //same to: open == false
open = true;
Debug.Log("yeah");
//your code, change position
Instance.transform.position = openPosition;
/* other (probably useless) things
* 1- disabling it
* Instance.SetActive(false);
* 2 - change to mouse position
* Instance.transform.position = Camera.main.ScreenToWorldPoint(
* Input.mousePosition, Vector3.Distance (Instance.transform.position, camera.main));
*/
} else {
//added to return to default when press right click again
open = false;
Instance.SetActive(true);
Instance.transform.position = prefab.transform.position;
}
}
}
source: the useless part myself, the rest last answer (from @FlightCrazed ) here
Your answer
![](https://koobas.hobune.stream/wayback/20220612192051im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Text not displaying public int but will display private int 1 Answer
Can anyone point me to a good tutorial for Gear VR media touch controls? 0 Answers
What is the code I would use to make a UI button start an animation? 1 Answer
Use a canvas prefab multiple times at the same time 0 Answers
Setting anchors in RectTransform making panel appear to vanish 1 Answer