- Home /
how to get value from other script?
I have 2 Script :
EnemyAI [this C#]
EnemyHealt [this JavaScript]
I want to get value CurrentHealt from EnemyHealt to EnemyAI. How to do that?
NB: Sorry for my bad english.
Answer by VioKyma · Aug 17, 2013 at 03:13 AM
You will have problems going from UnityScript (JavaScript) to C# because they are actually stored in different projects. I would highly recommend using one or the other unless you have no other choice.
Basically, you want to find the object, then reference the script and value. For example if the script was on the same object:
EnemyHealth enemyHealth = GetComponent<EnemyHealth>();
// Get current health
enemyHealth.CurrentHealth;
Or if they are on different objects:
GameObject enemy = GameObject.Find("Enemy");
EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();
// Get current health
enemyHealth.CurrentHealth;
so, that is imposible to do. O$$anonymous$$ thank you very much.
Not impossible, Unity documentation states that:
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.
It is fiddly but can be done. You can find more on it here (and in the rest of that section): https://docs.unity3d.com/$$anonymous$$anual/ScriptCompileOrderFolders.html
Awesome, I was waste my time with this problem and your sollution solve it. Thank You
This did not work for me, I have posted another link to a solution that does