- Home /
How to make a bunch of items using the same variables?
I'm sure this has been asked many times before, and I apologize I couldn't find exactly what I'm looking for.
What I want is a list of items in Javascript that all share the same variables but different values that I can call at times to assign to an item.
Ex. In Pseudo code kinda
Steel Sword { var damage = 10; var cost = 100; var rarity = 1; }
The Bane { var damage = 1200; var cost = 100000; var rarity = 95; }
Then have a script attached to the actual game object that calls for the stats from the group for the item.
Please state where your actual problem occurs. The described functionality could be achieved by many different means. I think most likely you want to read about deriving classes and sharing properties between them.
Answer by Berenger · Jun 11, 2012 at 02:57 PM
In Unity, you can create script that behave like components. They actually are components, but I meant you can use them like Unity components (colliders, mesh renderer, rigidbody etc). You attach them, and they will do something.
In UnityScript (visciously called javascript, or js), you don't need to understand what's going on, you just declare variables, implement special functions, check if it compiles then drag the script on a gameObject. You will then see the variables (the public ones) in the inspector and Unity will call those functions.
In C#, you need to create a class inheriting from MonoBehaviour (js is doing it implicitely), and then declare variables, implement function.
Bottom line, you need a script to attach to a gameObject. Your too swords will have that same script but will differe by the variables value in the inspector (and probably mesh / material). Here is an example of those class :
// JS
var damage : int = 10;
var cost : int = 100;
var rarity : int = 1;
//C#
using UnityEngine; // Access to MonoBehaviour
public class Sword : Monobehaviour
{
public int damage = 10; // Note that C# vars are private by default
public int cost = 100;
public int rarity = 1;
}