( Sorry for my bad english) So I’m following the text 101 tutorial of the Complete C# Unity Game Developer 2D course and I want to add images to my game. I want the image to be displayed and change every state but it fails. I tried doing it myself but it doesn’t show anything in the game screen
Hers’s my code:
AdventureGame.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.UIElements;
public class AdventureGame : MonoBehaviour
{
[SerializeField] Text textComponent;
[SerializeField] State startingState;
public SpriteRenderer spriteRenderer;
State state;
void Start()
{
state = startingState;
textComponent.text = state.GetStateStory();
}
void Update()
{
ManageState();
}
private void ManageState()
{
var nextState = state.GetNextState();
for(int i=0;i<nextState.Length;i++)
{
if (Input.GetKeyDown(KeyCode.Alpha1+i))
{
state = nextState[i];
spriteRenderer.sprite = state.GetStateImage();
}
}
spriteRenderer.sprite = state.GetStateImage();
textComponent.text = state.GetStateStory();
}
}
State.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.UIElements;
[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject
{
[TextArea(10,14)] [SerializeField] string storyText;
[SerializeField] State[] nextState;
public Sprite storyImage;
public string GetStateStory()
{
return storyText;
}
public State[] GetNextState()
{
return nextState;
}
public Sprite GetStateImage()
{
return storyImage;
}
}