How to count number of sprites with a specific name
Hi all,
I have a game where you collect different numbers of stars depending on your score. When the star gameobject is active, the star is either grey (when the score has not been reached) or gold (when a score is reached). The stars share the same gameobject and therefore the colour change is due to a change in sprite not activity of the gameobject. I am unsure how i can count the number of gold stars alone. Is there a way similar to finding objects with tags, but instead finding objects with a specific sprite name in a scene and counting them.
I believe I could count all the stars if I assign a tag to the star gameobjects all and count the number of stars with the specific tag and also active.
Thanks
Answer by Larry-Dietz · Dec 19, 2019 at 03:31 AM
You can just use the FindGameObjectsWithTag
Wherever you are enabling and disabling these objects, change the tag at the same time, then you can use FindGameObjectsWithTag for whichever you need.
Hope this helps, -Larry
Thanks for your reply. The only problem I have with this solution is that the enabling/disabling of the object only makes the star appear/disappear and does not effect the colour. The colour of both grey and gold version of the star are only visible if the object is enabled. Therefore any tag i use becomes attached to both the grey and gold star as it is the same object with just a change in source image. So I was wondering if there is a way to detect when the source image of the object is changed.
Thanks again for your time and help.
This idea should still work. At some point in your code, presumably, you are setting/changing the source image in the star, correct? Just change the tag at that point as well.
If you would like, I am willing to take a look at your code, and help you figure out where/how to get the tags changed, and get a count of each color star.
-Larry
Hi Larry,
I am only aware of using tags in the inspector and not in script if that is what you mean.
I have attached the script in question to clear things up.
public class stars : $$anonymous$$onoBehaviour { public int numLevel;
[SerializeField]
private Sprite goldStarSprite;
[SerializeField]
private Text numLevelTextBlue;
[SerializeField]
private Text numLevelTextPink;
[SerializeField]
private GameObject star1;
[SerializeField]
private GameObject star2;
[SerializeField]
private GameObject star3;
private void Start()
{
numLevelTextBlue.text = numLevel.ToString();
numLevelTextPink.text = numLevel.ToString();
var nextLevel = PlayerPrefs.GetInt("next_level");
if (nextLevel == 0)
{
nextLevel = 1;
}
if (numLevel == nextLevel)
{
buttonImage.sprite = currentButtonSprite;
star1.SetActive(false);
star2.SetActive(false);
star3.SetActive(false);
numLevelTextPink.gameObject.SetActive(false);
}
else if (numLevel < nextLevel)
{
numLevelTextBlue.gameObject.SetActive(false);
var stars = PlayerPrefs.GetInt("level_stars_" + numLevel);
switch (stars)
{
case 1:
star1.GetComponent<Image>().sprite = goldStarSprite;
break;
case 2:
star1.GetComponent<Image>().sprite = goldStarSprite;
star2.GetComponent<Image>().sprite = goldStarSprite;
break;
case 3:
star1.GetComponent<Image>().sprite = goldStarSprite;
star2.GetComponent<Image>().sprite = goldStarSprite;
star3.GetComponent<Image>().sprite = goldStarSprite;
break;
}
}
else
{
numLevelTextBlue.gameObject.SetActive(false);
numLevelTextPink.gameObject.SetActive(false);
star1.SetActive(false);
star2.SetActive(false);
star3.SetActive(false);
}
}
Thanks again for the help.
Answer by jihadkhawaja · Dec 21, 2019 at 01:06 AM
You can use GameObject.Find https://docs.unity3d.com/ScriptReference/GameObject.Find.html
However u should have the gameobject enabled in the scene in order to find it, and then u can enable the gameobject sprite components. Hope this helps