0
\$\begingroup\$

I am following a tutorial called Complete C# Unity Developer 2D: Learn to Code Making Games. In one section we create a text-based game, where we display a canvas with some UI elements to the user. The text changes based on the current state, which is setup through a couple of scripts. Just for future knowledge (it isn't part of the tutorial), I want to make an image display for each state.

Before I post the scripts, here is what I have tried: 1) I serialized a Sprite field in one of the scripts so that I could assign a .jpg asset sprite to it. At that point I was not sure how to get it to appear based on the state, which is an array of states. 2) I created a UI Image and added it to the canvas, then disabled it. My intent here was to enable it when the state.name == x. Unfortunately, I can't seem to get that to work either.

I am using two scripts, 1) AdventureGame.cs & 2) State.cs:

1) AdventureGame.cs

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

public class AdventureGame : MonoBehaviour
{
    [SerializeField]
    Text m_textComponent;
    [SerializeField]
    State startingState;
    // I added this Sprite field
    [SerializeField]
    Sprite m_spriteComponent;

    State state;


    void Start()
    {
        state = startingState;
        m_textComponent.text = state.GetStateStory();
        // This is what I added, a call to a method in the other script
        m_spriteComponent = state.GetStateSprite();
        Debug.Log(m_spriteComponent);
        // The sprite is called HellDoor
    }

    void Update()
    {
        ManageState();
    }

    private void ManageState()
    {
        var nextStates = state.GetNextStates();
        for (int index = 0; index < nextStates.Length; index++)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1 + index))
            {
                state = nextStates[index];
            }
            m_textComponent.text = state.GetStateStory();

        }
        // Tested for state name to be sure, Door is one of them

        Debug.Log(state.name);
        if (state.name == "Door")
        {
            // Not sure what to put here
        }

    }

}

2) State.cs

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

// Creates a new slot called State in the Create menu!
[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject
{
    // First number determines minimum size of the field, and the second number
    // determines the amount of lines before you start to scroll
    [TextArea(10, 14)]
    [SerializeField]
    string m_storyText;
    [SerializeField]
    State[] nextStates;
    // This is the field I added
    [SerializeField]
    Sprite m_stateSprite;

    public string GetStateStory()
    {
        return m_storyText;
    }

    public State[] GetNextStates()
    {
        return nextStates;
    }
    // This is the method I added
    public Sprite GetStateSprite()
    {
        return m_stateSprite;
    }
}

Additionally, this is what I have setup in the editor.

Editor Layout

States

\$\endgroup\$
0
\$\begingroup\$

I am not familiar with unity but as far as I can see you only call m_spriteComponent = state.GetStateSprite(); inside the create Method.

When is the current state drawn ( where do you find it in code ) ? Make sure the variable and everything that goes with it is set accordingly when you register touch down events.

Also I wonder about the array of states. Where is it initialized? But maybe I am wrong about this and it is handled by unity.

\$\endgroup\$
2
  • \$\begingroup\$ I figured it out, but thanks. I'm on my mobile, otherwise I'd post solution. I think what you said is accurate. \$\endgroup\$ Sep 4 '19 at 6:47
  • \$\begingroup\$ Nice! Thanks for the feedback and good luck! \$\endgroup\$
    – yesyoor
    Sep 17 '19 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .