- Home /
[JS] Adding a scripts variable to a variable
Hey all! This might be a little confusing but I will try my best to explain.
I know you can add scripts to variables e.g.
var stats : ScriptName;
And then you assign it with either more code or in the inspector.
However, what I want is to assign a variable from ScriptName to a variable on another script.
For example if Script1 had a variable named ElfStats.
Instead of adding
var stats : Script1;
Which gets the script, I want to get the ElfStats variable by assigning it in the inspector.
I don't want to use stats.ElfStats because my plan is to have one main script which stores all the data about every character in their own variable. For example on Script1 there will be:
var character : String[] = ["Player", "5", "6"];
var elf : String[] = ["Elf", "3", "2"];
var demon : String[] = ["Demon", "6", "8"];
var ork : String[] = ["Ork", "1", "10"];
And then on each character I will assign Script2 there will be:
var stats : ScriptWithVariable;
Which then can be accessed from Script2:
rigidbody.AddForce(transform.forward * stats[5]);
//It will be just stats, because the stats variable will contain the other variable.
This way, when you are programming a characters stats, you only need one script, so you are not looking through many and each one find its corresponding variable.
So you are literally assigning a variable from another script, as a variable.
I really hope you understand me and maybe can help me! Thank you all :)
Your plan is not good. A better plan was invented long ago: Object-Oriented Program$$anonymous$$g. You should go learn about it and then revisit your script.
I don't know why this has a negative vote, I have seen far worse questions here. Even though I don't fully understand the question, I am upvoting to remove negative vote.
I was going to suggest as a complete guess Class and Inheritance, but I really don't know anything about them.
I started to type up an example, but again I really don't know about technical stuff (am completely self-taught. OOP, Inheritance, Serialization, I've tried to understand but my brain flips out!).
If I leave this as an incorrect example, maybe someone will come along and help clear the matter up.
public class $$anonymous$$onster extends $$anonymous$$onoBehaviour
{
var name : String = "Creature";
var attack : int = 5;
var defense : int = 10;
}
public class Characters extends $$anonymous$$onster
{
var elf : $$anonymous$$onster;
var demon : $$anonymous$$onster;
function Start()
{
elf = new $$anonymous$$onster();
elf.name = "Elf";
elf.attack = 3;
elf.defense = 2;
demon = new $$anonymous$$onster();
demon.name = "Demon";
demon.attack = 6;
demon.defense = 8;
}
}
Currently this example gives the error : Class and Inheretance/Characters.js(11,26): BCE0031: Language feature not implemented: Ambiguous($$anonymous$$onster.name, UnityEngine.Object.name).
Anyway, at least this serves as a bump.
I just came here... saw var character : String[] = ["Player", "5", "6"];
and decided to post: Y U NO USE CLASSES :O!!!
Answer by Zuwolf · Jun 25, 2013 at 09:37 AM
If i understand, the goal is to store all your characters stats in one script. So why don't you put a same tag on your elfs for example and do GetComponent(script). You will get the script variable and you will be able to get each stats of each character. Then store it in a two dimension array ?
Thank you Zuwolf for your help! I think this is a much better way than what I originally thought of! Thank you everyone too! :)
Your answer
Follow this Question
Related Questions
Adding variables from all scripts 2 Answers
[SOLVED] Problem with "foreach". 1 Answer
Access variables, objects in array in another script? 2 Answers
script communicate with each other 0 Answers