Namespace issue

Hi! normally when I press Play button, the game play fine. But when I Build the game it has error.

Looks like you’re trying to use classes from the UnityEditor namespace in a build, which you can’t do. Looking at those errors, you may be able to fix them by using RuntimeAnimatorController (which exists in runtime and is derived from AnimatorController) instead of AnimatorController (which isn’t directly usable in runtime). If the solution isn’t that simple we’d need to see the code that’s causing this error to help.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Animations;
using UnityEngine;
using UnityEditor;
using Mirror;
public class Avatar : NetworkBehaviour
{
    public GameObject character;
    public Sprite[] characterChoice;
    [SyncVar]
    public int score;

    public AnimatorController[] animeControl;
    void Start()
    {

        character.GetComponent<SpriteRenderer>().sprite = characterChoice[1];
        character.AddComponent<Animator>();
        character.GetComponent<Animator>().runtimeAnimatorController = animeControl[1];
    }

to allow player to choose different character, all animations has to change accordingly.

Pretty sure you just need to remove the using UnityEngine; line.

CORRECTION: I meant,

"You need to remove the using UnityEditor;

Muscle memory made me type Engine… sorry! Also, see other blurb below if you DO need the editor…

1 Like

remove the using UnityEngine will has many error.

Is there any reason to use a AnimatorController[ ] instead of a RuntimeAnimatorController[ ]?

If you want to commingle editor code and runtime code, you need to encase it with conditional compiler statements:

#if UNITY_EDITOR
using UnityEditor;
#endif

Same goes for any lines that rely on editor namespace. This approach lets you use a single file for a class and its custom editor, for instance.

Thank you! The controller problem solved. But Animation issue still exist.
error CS0234: The type or namespace name ‘Animations’ does not exist in the namespace ‘UnityEditor’ (are you missing an assembly reference?)

Remove the using UnityEditor.Animations; line as well.

And you do still need to change line 14 there to be RuntimeAnimatorController and not AnimatorController, as mentioned in my first comment.

working. Thank you everyone’s help:):):slight_smile: Very Helpful