Error cs0029 cannot implicitly convert type 'state[]' to 'state' Please Help

Hello I’ve been taking the course on unity 2d and when I got to the section three video 27
it is displaying the error "error cs0029 cannot implicitly convert type ‘state’ to ‘state’ ".Please help with it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "state")]
public class state : ScriptableObject
{
    [TextArea(30, 10)] [SerializeField] string StoryText;
    [SerializeField] state[] NextStates ;
    public string GetStateStory()
    {
        return StoryText;
    }
    public state GetNextState()
    {
        return NextStates;
    }
}

The error is showing at the

        return NextStates;

Hi Anwin,

Welcome to our community! :slight_smile:

Check the returntype of GetNextState. Does it match the type of your NextStates variable? Hint: It doesn’t.

I’m sorry I didn’t quite understand…I’m totally new to coding…so…

You have this line:

[SerializeField] state[] NextStates ;

The variable is of type state[], not state.

The return type of your method is state instead of state[]: public state GetNextState().

A method can return the type defined in its signature only.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

No I’ll check it out

Thank you. I was stressing more on the public. I understand now.

Fantastic! :slight_smile:

public is the access modifier. void or, in your case, state is the return type of a method.

Is everything working now?


See also:

I am still getting the same error in unity even though no error is being displayed in visual studio…and some more errors came…

error CS0021: Cannot apply indexing with to an expression of type ‘state’

 private void ManageState()
    {
        var NextStates = State.GetNextState();
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            State = NextStates[0];
        } else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            State = NextStates[1];
        } else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            State = NextStates[2];
        }
        TextComponent.text = State.GetStateStory();
    }
}

Could you share your entire formatted code?

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

public class AdventureGame : MonoBehaviour
{
    [SerializeField] Text TextComponent;
    [SerializeField] state StartingState;
    state State;
    // Start is called before the first frame update
    void Start()
    {
        State = StartingState;
        TextComponent.text = State.GetStateStory();
    }
    //xx
    // Update is called once per frame
    void Update()
    {
        ManageState();
    }

    private void ManageState()
    {
        var NextStates = State.GetNextState();
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            State = NextStates[0];
        } else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            State = NextStates[1];
        } else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            State = NextStates[2];
        }
        TextComponent.text = State.GetStateStory();
    }
}

Now that’s Gone too…but what is “Object reference not set to an instance of an object”

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms