NameSpace player health
Hi I have that code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class HUD : MonoBehaviour
{
public Sprite[] HeartSprites;
public Image HeartUI;
private Character character;
void Start()
{
character = GameObject.FindGameObjectsWithTag("Player").GetComponent<Player>();
}
}
and it sent me an error: The type or namespace name `Character' could not be found. Are you missing a using directive or an assembly reference?
My "Player" script was made in Javascript and called "Character" What Can I do? I'm making the health system from this tutorial: https://www.youtube.com/watch?v=5KwkfGfaRNU
But All my scripts was made by me, and I didn't make anything but Health from this tutorial
Answer by Landern · Aug 10, 2016 at 05:13 PM
You're mixing Javascript(unityscript) with c#, you need to ensure the .js code is compiled before the c# to resolve references. There are special folders for such occasions and are listed in the Unity3d Manual. Please place your .js code in one of the folders to ensure the type can resolve in c#.
Special Folders and Script Compilation Order
Edit: To the question of "where are these folders", if they don't exist... Create one from phase 1(like plugins) so the .js is build first resulting in them being available as types to the c# code.
Where are these folders, Do you mean that these two scripts need to be in one folder? If yes - I made it.
But If no, I don't understand what to do
Did you read manual page i linked? It explains everything, if not i will copy and paste the information right here:
For the most part, you can choose any names you like for the folders in your project but Unity reserves some names to indicate that the contents have a special purpose. Some of these folders have an effect on the order of script compilation. Essentially, there are four separate phases of script compilation and the phase where a script will be compiled is deter$$anonymous$$ed by its parent folder.
This is significant in cases where a script must refer to classes defined in other scripts. The basic rule is that anything that will be compiled in a phase after the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.
Another situation occurs when a script written in one language must refer to a class defined in another language (say, a UnityScript file that declares variables of a class defined in a C# script). The rule here is that the class being referenced must have been compiled in a earlier phase.
The phases of compilation are as follows:
Phase 1: Runtime scripts in folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 2: Editor scripts in folders called Editor that are anywhere inside top-level folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 3: All other scripts that are not inside a folder called Editor.
Phase 4: All remaining scripts (ie, the ones that are inside a folder called Editor).
A common example is where a UnityScript file needs to reference a class defined in a C# file. You can achieve this by placing the C# file inside a Plugins folder and the UnityScript file in a non-special folder. If you don’t do this, you will get an error saying the C# class cannot be found.
Note. Standard Assets work only in the Assets root folder.