- Home /
The image of the button disappears everytime when it is clicked. Where to find in this code? I'm just following tutorials on youtube and this guy speaks indonesia so I didn't kinda understand.
WordScramble.cs
[System.Serializable] public class Result { public int totalScore = 0; [Header("REF UI")] public Text textTime; public Text textTotalScore;
[Header("REF RESULT SCREEN")]
public GameObject resultCanvas;
public Image[] stars;
public Text textResultScore;
public Text textInfo;
[Space(10)]
public Color starOn;
public Color starOff;
public void ShowResult()
{
textResultScore.text = totalScore.ToString();
textInfo.text = "You finished " + WordScramble.main.words.Length + " words!";
int allTimeLimit = WordScramble.main.GetAllTimeLimit();
for (int i = 0; i < stars.Length; i++)
{
stars[i].color = totalScore >= allTimeLimit / (3 - i) ? starOn : starOff;
}
resultCanvas.SetActive(true);
}
}
[System.Serializable] public class Word { public string word; [Header("leave empty if you want randomized")] public string desiredRandom;
[Space(10)]
public float timeLimit;
public string GetString()
{
if (!string.IsNullOrEmpty (desiredRandom))
{
return desiredRandom;
}
string result = word;
result = "";
while (result == word)
{
List<char> characters = new List<char> (word.ToCharArray ());
while (characters.Count > 0)
{
int indexChar = Random.Range (0, characters.Count - 1);
result += characters [indexChar];
characters.RemoveAt (indexChar);
}
}
return result;
}
}
public class WordScramble : MonoBehaviour {
public Word[] words;
[Space(10)]
public Result result;
[Header("UI REFERENCE")]
public GameObject wordCanvas;
public CharObject prefab;
public Transform container;
public float space;
public float lerpSpeed = 5;
List<CharObject> charObjects = new List <CharObject>();
CharObject firstSelected;
public int currentWord;
public static WordScramble main;
public static float totalScore;
void Awake()
{
main = this;
}
// Use this for initialization
void Start () {
ShowScramble (currentWord);
result.textTotalScore.text = result.totalScore.ToString ();
}
// Update is called once per frame
void Update ()
{
RepositionObject ();
totalScore = Mathf.Lerp (totalScore, result.totalScore, Time.deltaTime * 5);
result.textTotalScore.text = Mathf.RoundToInt (totalScore).ToString ();
}
public int GetAllTimeLimit()
{
float result = 0;
foreach (Word w in words)
{
result += w.timeLimit / 2;
}
return Mathf.RoundToInt(result);
}
//<summary>
//show word from collection with desired index
//</summar>
void RepositionObject()
{
if (charObjects.Count == 0)
{
return;
}
float center = (charObjects.Count - 1) / 2;
for (int i = 0; i < charObjects.Count; i++)
{
charObjects [i].rectTransform.anchoredPosition = Vector2.Lerp(charObjects[i].rectTransform.anchoredPosition, new Vector2 ((i - center) * space, 0), lerpSpeed * Time.deltaTime);
charObjects [i].index = i;
}
}
//Show a random word to the screen
public void ShowScramble()
{
ShowScramble (Random.Range (0, words.Length - 1));
}
//show word from collection with desired index
public void ShowScramble(int index)
{
charObjects.Clear ();
foreach (Transform child in container)
{
Destroy (child.gameObject);
}
//WORDS FINISHED
//SHOW RESULT SCREEN
if (index > words.Length - 1)
{
result.ShowResult();
wordCanvas.SetActive(false);
//Debug.Log ("index out of range, please enter range between 0-" + (words.Length - 1).ToString());
return;
}
char[] chars = words [index].GetString ().ToCharArray ();
foreach (char c in chars)
{
CharObject clone = Instantiate (prefab.gameObject).GetComponent<CharObject> ();
clone.transform.SetParent (container);
charObjects.Add (clone.Init (c));
}
currentWord = index;
StartCoroutine (TimeLimit());
}
public void Swap (int indexA, int indexB)
{
CharObject tmpA = charObjects [indexA];
charObjects [indexA] = charObjects [indexB];
charObjects [indexB] = tmpA;
charObjects [indexA].transform.SetAsLastSibling ();
charObjects [indexB].transform.SetAsLastSibling ();
CheckWord ();
}
public void Select(CharObject charObject)
{
if (firstSelected)
{
Swap (firstSelected.index, charObject.index);
//Unselected
firstSelected.Select ();
charObject.Select ();
}
else
{
firstSelected = charObject;
}
}
public void UnSelect()
{
firstSelected = null;
}
public void CheckWord()
{
StartCoroutine (CoCheckWord ());
}
IEnumerator CoCheckWord ()
{
yield return new WaitForSeconds (0.5f);
string word = "";
foreach (CharObject charObject in charObjects)
{
word += charObject.character;
}
if (timeLimit <= 0)
{
currentWord++;
ShowScramble (currentWord);
yield break;
}
if (word == words [currentWord].word)
{
currentWord++;
result.totalScore += Mathf.RoundToInt (timeLimit);
//StopCoroutine (TimeLimit ());
ShowScramble (currentWord);
}
}
float timeLimit;
IEnumerator TimeLimit()
{
timeLimit = words [currentWord].timeLimit;
result.textTime.text = Mathf.RoundToInt (timeLimit).ToString ();
int myWord = currentWord;
yield return new WaitForSeconds (1);
while (timeLimit > 0)
{
if (myWord != currentWord) { yield break; }
timeLimit -= Time.deltaTime;
result.textTime.text = Mathf.RoundToInt (timeLimit).ToString ();
yield return null;
}
CheckWord ();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
CharObject.cs
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using System.Collections.Generic;
public class CharObject : MonoBehaviour {
public char character;
public Text text;
public Image image;
public RectTransform rectTransform;
public int index;
[Header("Appearance")]
public Color normalColor;
public Color selectedColor;
bool isSelected = false;
public CharObject Init (char c)
{
character = c;
text.text = c.ToString ();
gameObject.SetActive (true);
return this;
}
public void Select()
{
isSelected = !isSelected;
image.color = isSelected ? selectedColor : normalColor;
if (isSelected) {
WordScramble.main.Select (this);
} else {
WordScramble.main.UnSelect ();
}
}
}
you want to change color of an image when you click on it? or it's a button ?:)
Answer by PersianKiller · Sep 03, 2017 at 06:43 AM
I want to offer you an easy solution. create a UI button,and put your image in the button, so you don't need to write codes to change color of image when you click on it.beacuse it's a button :) and unity do it for you.just change the value of Pressed Color.
Hi! I'm just new to Unity. Yes, it is a button. However, it still doesn't work :(
Answer by SohailBukhari · Sep 03, 2017 at 08:38 AM
The Button Not changing from the code its changing from the inspector. When you set your button property color tint to Sprite Swap then option of sprites enable.
So check for sprite in the inspector instead of code.
Hello, I tried dragging different image to the highlight and it works. However, when, I clicked, it disappears even though it has a sprite. I'm new to unity.
I also tried dragging in the Pressed, still, it disappears.
did you read the button event ? if not then check where your sprite is changing. we can't tell anything as we dont have the project.
Answer by viamarie · Oct 03, 2017 at 08:54 AM
just change the normal color to #FFFFFFFF
Your answer
Follow this Question
Related Questions
Unity ads , can't we show video by clicking a button? 1 Answer
Destroy a Gameobject with a UI Button 3 Answers
Add force when button is pressed 2 Answers
GameObject.Destroy(gameObject) does not destroy capsules 2 Answers
how to make a non ui button 1 Answer