1

So im trying to make a Text adventurer game and i want to change the background of the game when a specific dialogue shows up. I already have a black Image at the start and want to change it throughout the game.

I've made a scriptable object for the dialogues.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{

[TextArea(10, 14)] [SerializeField] string dialogueText;
[SerializeField] string hintText;
[SerializeField] Dialogue[] nextDialogue;
[SerializeField] public Image background; // Thats the old Image that I want to change
[SerializeField] public Sprite newBackground; // Thats the new Background I want to put in Image


public bool dialogueHolder;

public void Awake()
{
    if (background != null)
    {
        background.sprite = newBackground; // this code is not working
    }
}
public string getHintText()
{
    return hintText;
}
public string GetDialogue()
{
    return dialogueText;
}

public Dialogue[] GetNextDialogue()
{
    return nextDialogue;
}

2 Answers2

0

Like Bugfinder commented, you haven't stated what's going wrong and what you have tried so far. However, looking at your code, I can see a few things that will definitely go wrong.

First of all, you have an Awake method inside a scriptable object. This will do nothing, by itself because Awake (among others) is only called on MonoBehaviour objects.

I would rename the method, for logic sake, into something like "Activate" and then call it from a manager (DialogueManager?) script which should be responsible for handling dialogues.

Also, I imagine that you have incorrectly pasted your code. As it is now it would certainly give compile errors.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{

// these lines are duplicate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{
Immorality
  • 2,164
  • 2
  • 12
  • 24
0
private void ManageDialogue()
{
    var nextStates = startingDialogue.GetNextDialogue();

    for (int i = 0; i < nextStates.Length + 1; i++)
    {
            startingDialogue = nextStates[0];
    }
    if (animatedText.isActionPerformed == false) { animatedText.AnimateText(startingDialogue.GetDialogue()); }
    if (startingDialogue.background != null) { startingDialogue.background.sprite = startingDialogue.newBackground;}
}

so this is how i would implement my code but

startingDialogue.background.sprite = startingDialogue.newBackground

doesnt seem to work for me. It keeps being the same Image as before.

  • It wasnt the code. The Image was dark and I had to make it brighter to see the image. LOL –  Aug 01 '19 at 00:54