Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Namespace issue

Discussion in 'Scripting' started by gamer123454321, Nov 23, 2020.

  1. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    Hi! normally when I press Play button, the game play fine. But when I Build the game it has error.
    question1.png
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    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.
     
    Joe-Censored likes this.
  3. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Animations;
    4. using UnityEngine;
    5. using UnityEditor;
    6. using Mirror;
    7. public class Avatar : NetworkBehaviour
    8. {
    9.     public GameObject character;
    10.     public Sprite[] characterChoice;
    11.     [SyncVar]
    12.     public int score;
    13.  
    14.     public AnimatorController[] animeControl;
    15.     void Start()
    16.     {
    17.  
    18.         character.GetComponent<SpriteRenderer>().sprite = characterChoice[1];
    19.         character.AddComponent<Animator>();
    20.         character.GetComponent<Animator>().runtimeAnimatorController = animeControl[1];
    21.     }
    22.  
     
  4. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    to allow player to choose different character, all animations has to change accordingly.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,754
    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...
     
    Last edited: Nov 24, 2020
  6. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    remove the using UnityEngine will has many error.
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Is there any reason to use a AnimatorController[] instead of a RuntimeAnimatorController[]?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,754
    If you want to commingle editor code and runtime code, you need to encase it with conditional compiler statements:

    Code (csharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. #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.
     
  9. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    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?)
     
  10. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    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.
     
  11. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    working. Thank you everyone's help:):):) Very Helpful