Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Functions in unityscript

I just have a basic question. I'm trying to learn how to use functions with unityscript and I learned that when you create a function using something like

function Example(text)
{
    print(text);
}

and then call it up using

Example();

you can actually insert a string or a number into the parentheses on the latter piece of code in the parentheses and it will pass through and insert itself into the function you created previously. In this case, it does end up printing whatever I type into the parentheses.

Now, my question is if that works, then why don't functions where I pass in two numbers and ask to have them added work? The code I'm trying to use looks like:

Addition(4, 4);
function Addition(a, b)
{
    print(a+b);
}

When I try to execute this, Unity tells me

Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'Object'.

I was watching this video on Javascript functions and in the video at around 9 minutes, the guy types in exactly that, and gets the script to add it for him. I'm rather new to scripting and I do not have much of an idea of what I could be doing wrong. Thanks in advance for the help.

like image 904
Toushinu Avatar asked Feb 28 '13 04:02

Toushinu


People also ask

What is event function Unity?

Unity events are used in the UI system for linking events (such as On Click of Button) to an action (a function in a script). These can be saved with the scene and are editable from the inspector, unlike the vanilla C# events and delegates. These are both found in the UnityEngine. Events namespace.

What is the function of inspector in Unity?

The Inspector is used to view and edit object properties and also preferences and other settings within Unity.

Which function can be called more than once per frame?

FixedUpdate: FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate.


1 Answers

Actually, in Unity your code would have to look like:

Addition(4, 4);
function Addition(a:int, b:int)
{
    print(a+b);
}

You'd have to call what type of argument you expect in the function's parameters. You are completely right by stating that in JavaScript this normally isn't needed. In Unity, it is though.

This is because it is actually called UnityScript. UnityScript is the name used by people who want to point out that Unity's Javascript is very different from the traditional version of Javascript used in browsers.

I have found a nice list of differences for you between UnityScript and Javascript here. When I create a quick bulletlist out of that web page, I can tell you the differences are (perhaps not even all):

  • JavaScript is class-free
  • JavaScript supports multiple variable declarations in one var statement.
  • In JavaScript, assignment is treated as an expression.
  • Every top-level variable in JavaScript is global. Additionally, any variable declaration not preceded by the var statement is global.
  • In Javascript, dynamic typing is efficient.
  • In JavaScript, privacy is rather unconventional.
  • Dollar signs ($) are not allowed in UnityScript identifiers as they are in JS identifiers.
  • There is no with statement in UnityScript.
  • Javascript supports Regular Expression Literals (RegExp or regex).
  • In JavaScript, this can refer to one of two things, depending on the context:
    • The global object (best not explained here)
    • The object to which the current method or constructor belongs

I have retagged your question to categorize it in UnityScript as well. StackOverflow has this to say about it:

Unity's JavaScript tries to follow the ECMAScript standard closely, it varies in many ways from other implementations of JavaScript that are based on the same standard. It is perhaps most similar to Microsoft's JScript, especially in that both are .NET languages. However, Unity's version was developed independently and there are a number of differences between the two.

I, too, had to find out the hard way. I messed up the two languages and got to this problem.

like image 149
Joetjah Avatar answered Sep 18 '22 23:09

Joetjah