Sprite portraits?

So my code is a nightmare, i havent even got to the part i consider hard yet, but i need help with this. Basically if a string of words contains a given word, it changes a gameobject meant to be a character portrait to a portrait with that name. Dont know how im gonna do that, but ill cross that bridge when i come to it, for now im making sure the code works when variables are hardcoded. Anyone know how i can change this sprite? simply trying it as it is throws me the error: “Cannot implicitly convert type ‘UnityEngine.Object’ to ‘UnityEngine.Sprite’. An explicit conversion exists (are you missing a cast?)”

  if (sentence.Contains("Wallace"))
        {
            Debug.Log(portrait);
            Debug.Log("wall is " + Resources.Load("wall"));

            portrait.GetComponent<SpriteRenderer>().sprite = (Resources.Load("wall"));
          

        }

you can set type of the resources that you load,

Thank you very much! I am dumbfounded as to why this is necessary, but it works. Thanks!

the default method returns:

that’s why the error, it cannot convert that loaded Object into Sprite:

On a similar note, can you spot any way i could get to work? Trying to set character portraits per line, while keeping it flexible:

 if (sentence.Contains(* + "Port"))
        {
            Debug.Log(* + "Port");
            Debug.Log("wall is " + Resources.Load("wall"));

            portrait.GetComponent<SpriteRenderer>().sprite = (Resources.Load<Sprite>(* + "Port"));
         
           

        }

for example, if the text string contains “HeroPort”, then it loads a sprite named “HeroPort”, but if the text string contains “VillainPort” then it pulls one named “VillainPort” etc. At the moment, each (* + “Port”) gives the error “Operator ‘+’ cannot be applied to operand of type ‘string’”

what this part should do?

(* + "Port")
  • is a multiply operator so cannot be used there (and cannot use “*” as a wildcard for strings)

could try something simpler like:

// this if can be removed also, if all the sentence strings are valid resource names
if (sentence.Contains("Port"))
{
                Debug.Log("sentence is "+sentence);
//                Debug.Log("wall is " + Resources.Load("wall"));
                portrait.GetComponent<SpriteRenderer>().sprite = (Resources.Load<Sprite>(sentence));          
}

(* + “Port”) is meant to check for anything that ends with “Port’”. As for this part portrait.GetComponent<SpriteRenderer>().sprite = (Resources.Load<Sprite>(sentence));
i dont think this would work for what im aiming for. Im aiming to be able to set the portrait for each individual sentence. For example, sentence 1 would be “HeroPort I am going to stop you!”, then sentence 2 would be “VillainPort It’s too late!”
something along the lines. I’d still have to figure out how to remove the “*Port” from the sentence afterwards so it doesnt look weird, but that can wait.

strings are quite easy to manipulate in c# so you can really do anything you want! (for example you could split the string from first space character, so then you splitted string would be array, where first item is:
“*Port”,
second item is rest of the string:
“It’s too late!”

but for better results, you might want to try to sending that sprite name some other way,
instead of embedding it into that main message string.

In that case, i am completely lost, and will probably have to start over. I followed brackeys tutorial, not knowing he did not account for the fact devs might want to use portraits per dialogue box to clarify who is talking.

Here, im going to post my code so that its more obvious what is going on. In DialogueManager:

public class DialogueManager : MonoBehaviour
{
    public Text DialogueText;

    GameObject TBox;

    public bool firsties = true;

    private Queue<string> Sentences;

    public GameObject portrait;  

    // Start is called before the first frame update
    void Start()

    {
        TBox = GameObject.FindGameObjectWithTag("TextBox");
        TBox.SetActive(false);
        portrait = GameObject.Find("portrait");

      
        Sentences = new Queue<string>();
    }

    public void StartDialogue (Dialogue dialogue)
    {
        if(TBox.activeInHierarchy == false)
        {
            //portrait.SetActive(false);
            TBox.SetActive(true);
        }
       

        Sentences.Clear();

  

        foreach (string sentence in dialogue.sentences)
        {
           

            Sentences.Enqueue(sentence);

           
        }

       

        DisplayNextSentence();
    }
    public void DisplayNextSentence()
    {
       
        if(Sentences.Count == 0)
        {
            EndDialogue();
            return;
        }


        string sentence = Sentences.Dequeue();
        DialogueText.text = sentence;

     


        /*  if (sentence.Contains("Wallace"))
          {
              //Debug.Log(* + "Port");
              Debug.Log("wall is " + Resources.Load("Wallace"));

              portrait.GetComponent<SpriteRenderer>().sprite = (Resources.Load<Sprite>("Wallace"));
              //portrait.SetActive(true);
              Debug.Log("wallace is " + portrait.activeInHierarchy);


          }
          */
    }

    public void EndDialogue()
    {
        firsties = true;

        portrait.GetComponent<SpriteRenderer>().sprite = (null);

        TBox.SetActive(false);

        Debug.Log("end of convo");

        Debug.Log("firsties is " + firsties);
        Debug.Log(TBox.activeInHierarchy);
    }
}

in DialogueTrigger:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue talk;
   
    private bool playerInRange;
    public GameObject cool;
    private DialogueManager scriptie;
 

    private void Start()
    {
       

        cool = GameObject.Find("DialogueManager");

        scriptie = cool.GetComponent<DialogueManager>();

        scriptie.firsties = true;

       
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Player")
        {
            playerInRange = true;

            //Debug.Log("Starting conversation");
             
                      
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if(collision.tag == "Player")
        {
            playerInRange = false;
        }
       
    }

    private void Update()
    {
        if (playerInRange == true && Input.GetKeyDown(KeyCode.Space))
        {
         
            Triggerdialogue();
        }

    }


    public void Triggerdialogue()
    {
       


        if (scriptie.firsties == false)
        {
            ContinueDialogue();
        }
        else
        {
            FindObjectOfType<DialogueManager>().StartDialogue(talk);

           

            scriptie.firsties = false;
        }
            
       

    }


    private void ContinueDialogue()
    {
       
        FindObjectOfType<DialogueManager>().DisplayNextSentence();
    }

   
}

in Dialogue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue
{
    [TextArea(3, 10)]
    public string[] sentences;
}

if its for following and learning from a tutorial, i’d take the easy way out and just split the string,
instead of rewriting the system. (unless you are interested to do that).

Okay, how exactly does string splitting work? I read the link but dont understand much of it at all. And how would i do it in my specific case?

oh yeah, split was not the best option (that would split on every space character),
try something like this, where you take first word until space, and then rest of the words:
Split string from first space | C# Online Compiler | .NET Fiddle (click run, see results below)

Thank you, i will try this later.

It worked! Thank you so, so, so much sir!