x


Make JavaScript throw a custom error?

Hey fellas,

I have a script that involves a weapon turret. It has two variables, "projectile" and "raycastImpact", both of which you can assign GameObjects. If you set the projectile variable, it will launch the projectile when it fires. If you set the raycastImpact variable, it will create that impact at the hit.point of a raycast.

What I want is for the compiler to throw an error if I set both the projectile and the raycastImpact variables. Is there a way to throw a custom error in unity javascript?

(I'm assuming there is no better way to organize the variables. There are a couple different variables related to each type of shot; from what I've gathered there is no way to make a group of variables visible in the inspector only if another variable is assigned...)

Thanks a ton; you guys are awesome!

more ▼

asked Aug 04 '12 at 11:04 AM

SergeantBiscuits gravatar image

SergeantBiscuits
181 7 14 18

Just a point - I've edited your question to change Java to JavaScript - they are very different languages and Unity doesn't support Java.

Aug 04 '12 at 11:07 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can get the game to throw an exception but you can't make the compiler throw one.

    throw new System.Exception("Something Is Wrong");

Or

    throw new System.ArgumentException("Something was set incorrectly");

Or

    class MyException extends System.Exception
    {
          var myData : SomeType;
    }

     var x = new MyException();
     x.myData = whatever;
     throw x;
more ▼

answered Aug 04 '12 at 11:09 AM

whydoidoit gravatar image

whydoidoit
33.7k 14 23 105

Not sure how you are creating this item - but the best way might be to have 2 static creator functions - one that takes each parameter so you can't pass both.

    static function CreateMyThing(Vector3 position, GameObject projectile) : GameObject
    {
       ...
    }

    static function CreateMyThing(Vector3 position, Vector3 hitPoint) : GameObject
    {
       ...
    }
Aug 04 '12 at 11:13 AM whydoidoit

Thanks for the replies. But when I try "throw new Exception(..." or "throw new ArgumentException(..." the compiler complains:

"Assets/Scripts/GUIManager.js(19,19): BCE0005: Unknown identifier: 'Exception'."

whydoidoit -- I'm instantiating the objects within Update() based on if statements.

Aug 05 '12 at 02:26 AM SergeantBiscuits

change it to "UnityException"

throw new UnityException("my custom exception");

Aug 05 '12 at 04:08 AM Seth Bergman

Sorry, or put System. In front of it.

Aug 05 '12 at 07:23 AM whydoidoit

Both "UnityException" and "System.Exception" do exactly what I wanted. Thanks fellas!

Aug 05 '12 at 07:28 AM SergeantBiscuits
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x2030
x204

asked: Aug 04 '12 at 11:04 AM

Seen: 581 times

Last Updated: Aug 05 '12 at 08:42 AM