Help with variables
I want to add three variables together, but I am having trouble. Like this:
"variable12 = variable1 + variable2;"
But, of course, JS doens't work like that and I should've used C#. Please, I need this for tomorrow on my schools science fair, I'm making a quiz.
var a = 1;
var b = 2;
var c = 3;
a = a + b + c;
Does that help?
Or do you want to add three strings?
@$$anonymous$$arcos42563
where are you calculating these variables at? In Start(), Awake(), or Update() function?
Answer by spinnerbox · Sep 23, 2015 at 03:30 PM
So to make things clear, JavaScript(UnityScript) in Unity is not true JavaScript it is C# that looks like JavaScript :) I don't have large experience in UnityScript(JavaScript in Unity) but this should be very easy to answer:
Create new Unity Project.
Create new empty game object.
Call it JSTest.
Press "Add component" button in the inspector and then select "New Script". Choose JavaScript format.
type this code below pragma inside:
code:
public var a = 1;
public var b = 2;
public var c = 3;
function Start () {
a += (b + c);
Debug.Log("a = " + a);
}
function Update () {
}
So each game object inherits from MonoBehaviour. In JavaScript you type #pragma strict but the interpreter knows which namspaces to import so you don't have to worry about that. Each game object has several default functions which get executed when the object is instantiated and while the game is running. Those can be Awake, Start, Update, FixedUpdate, OnGUI etc... To initialize in first frame use Awake(). To intialize when the object is instantiated use Start(). The public keyword makes the variables visible in the inspector like so: