- Home /
How to copy and paste the color from one button to another button Unity C#?,How to copy a color of a button and past it on another button?
I am writing a script to allow colors to be copied from one location to another. I am trying to copy a specific color that a button displays, then copy that color and paste it onto another button.
I have tried two different ways:
First Way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorSet : MonoBehaviour {
// gameobjects
public GameObject btn2;
// bools
public bool selbtn2 = false;
// to set the color
[SerializeField]
// color of other button
public Color ColorToSet;
public void ColorToSetBtnPressed(Color color)
{
if (selbtn2 == true) {
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
}
}
The Second Way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorSet : MonoBehaviour {
// gameobjects
public GameObject btn2;
// bools
public bool selbtn2 = false;
// to set the color
[SerializeField]
// color of other button
public Color ColorToSet = ColorSet.Instantiate;
public void ColorToSetBtnPressed(Color color)
{
ColorToSet = GetComponent<Image>().color;
if (selbtn2 == true) {
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
}
}
Both types does not copy or set the color of the button clicked and set the color onto the other button. What would be the best method to copy the color of a button and paste it onto another button?
,I am writing a script to allow colors to be copied from one location to another. I am trying to copy a specific color that a button displays, then copy that color and paste it onto another button.
I have tried two different ways:
First Way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorSet : MonoBehaviour {
// gameobjects
public GameObject btn2;
// bools
public bool selbtn2 = false;
// to set the color
[SerializeField]
// color of other button
public Color ColorToSet;
public void ColorToSetBtnPressed(Color color)
{
//ColorToSet = GetComponent<Image>().color;
if (selbtn2 == true) {
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
}
}
The Second Way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorSet : MonoBehaviour {
// gameobjects
public GameObject btn2;
// bools
public bool selbtn2 = false;
// to set the color
[SerializeField]
// color of other button
public Color ColorToSet = ColorSet.Instantiate;
public void ColorToSetBtnPressed(Color color)
{
ColorToSet = GetComponent<Image>().color;
if (selbtn2 == true) {
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
}
}
Both types does not copy or set the color of the button clicked and set the color onto the other button. What would be the best method to copy the color of a button and paste it onto another button?