How to turn String into UnityEngine.Sprite?

I’m very much a beginner as you can see. So inside my “SpriteChanger” script, I have two sprites: “Happy” and “Sad”. In my “GameControl” script, I have a string “currentMood” which starts out as “Happy”. But then I have a public void to change the “currentMood” into “Sad”. Now in the SpriteChanger script, I put this.GetComponent ().sprite = which works when I manually put in the name of the sprite. However, I do not want to have a bajillion of those if I had a lot of sprites I need to switch to. I need a way to have this.GetComponent ().sprite = take the literal input of “currentMood”. But Unity keeps saying that it cannot implicitly convert String to UnityEngine.Sprite.

Here’s the code:

GameControl{

public string currentMood = "Happy";

public void changeMood () {

	currentMood = "Sad";

}

/some function that calls changeMood somewhere here/

}

.
SpriteChanger {

public Sprite Happy;
public Sprite Sad;
/*and a buncha other sprites*/

public void changeMood () {
	this.GetComponent<SpriteRenderer> ().sprite = GameControl.instance.currentMood)
}

}

// Drag and drop the sprites in the editor
// Make sure the name of the sprites matches your mood
[SerializeField]
private Sprite moodSprites;

 private SpriteRenderer spriteRenderer;
 public void ChangeMood ()
 {
     if( spriteRenderer == null )
         spriteRenderer = GetComponent<SpriteRenderer> ()
     spriteRenderer.sprite = GetMoodSprite( GameControl.instance.currentMood());
 }
 private Sprite GetMoodSprite( string mood )
 {
      for( int i = 0 ; i < moodSprites.Length ; ++i )
      {
          if( moodSprites _!= null && string.Equals( moodSprites*.name, mood )*_

return moodSprites*;*
}
Debug.LogWarningFormat( “No sprite representing mood ‘{0}’ has been found”, mood );
return null;
}
If the sprites does not have the correct names, you could use a simple struct like this:
public struct MoodSprite
{
public string Mood;
public Sprite Sprite;
}
// Drag and drop the sprites in the editor
// Make sure the name of the sprites matches your mood
[SerializeField]
private MoodSprite[] moodSprites;

private SpriteRenderer spriteRenderer;
public void ChangeMood ()
{
if( spriteRenderer == null )
spriteRenderer = GetComponent ()
spriteRenderer.sprite = GetMoodSprite( GameControl.instance.currentMood());
}
private Sprite GetMoodSprite( string mood )
{
for( int i = 0 ; i < moodSprites.Length ; ++i )
{
if( moodSprites != null && string.Equals( moodSprites*.Mood, mood )*
return moodSprites*.Sprite;*
}
Debug.LogWarningFormat( “No sprite representing mood ‘{0}’ has been found”, mood );
return null;
}
----
If you really want to keep the way you were thinking about, you need reflection:
using System.Reflection;
// …

public Sprite Happy;
public Sprite Sad;
private SpriteRenderer spriteRenderer;
public void ChangeMood ()
{
if( spriteRenderer == null )
spriteRenderer = GetComponent ()
spriteRenderer.sprite = GetMoodSprite( GameControl.instance.currentMood());
}
private Sprite GetMoodSprite( string mood )
{
System.Type type = GetType();
FieldInfo info = type.GetField( mood );
if( info == null )
{
throw new System.Exception( “The mood '” + mood + “’ does not have any sprite associated” );
}
return (Sprite)info.GetValue(this);
}

private void Start()
{
for (int i = 0; i < spriteArray.Length; i++)
{
if (spriteArray*.name == spriteNameString)*
{
p1image.sprite = spriteArray*;*
break;
}
}
}
put sprites in inspector, and be sure spriteNameString matches with sprites name