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"));
}
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’”
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.
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).
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)