Jump to content
  • Advertisement
Sign in to follow this  
CaseyHardman

Unity Problems with arrays being set to classes

This topic is 2590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

I'm using UnityScript with a Unity3D game project. I've written about 400 lines of code for the Player script, and then I decided maybe it'd be a good idea to include #pragma strict at the start of the script, since I was told it's a good practice.

So I braced myself for the multitudes of errors coming my way, and now I'm stuck on a set of errors. The problem is that I have an array called 'skills' which I declare as so:

public var skills = new Array();


The only objects being added to skills are those of a class named Skill:



class Skill
{
var Hotkey:String;
var dub:String;
var Script:Skills;
}



Note: Script is typed to Skills, which is the name of a .js script. It's what causes the functionality of the skills.

Anyway, I use the skills array to handle all of the player's skills, so I refer to the variables in Skill. For example, I use this piece of code at one point:


if (Input.GetKeyDown(skills.Hotkey))



It's in a for loop, hence the 'i'. Lines like this, which refer specifically to variables that are a part of the Skill class, come up with the error stating that "Hotkey is not a member of Object". I suppose it's because the skills array is just an array. I can't use built-in Unity arrays because they've got other problems that I can't seem to get around.

Is there any way I could tell the compiler that I'm not going to assign an object in skills to anything other than a Skill class, or am I going to have to find a way around it by changing the way I check for hotkeys and such? Should I just not use #pragma strict..?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
have you tried an explicit cast like
[color=#000088]if[color=#000000] [color=#666600]([color=#660066]Input[color=#666600].[color=#660066]GetKeyDown[color=#666600](([color=#000000]skills[color=#666600][[color=#000000]i[color=#666600]] as skill).[color=#660066]Hotkey[color=#666600]))

also, might need to declare array as
var Arrayname: Array = new Array();

Regs,
Mike
@runonthespot

Share this post


Link to post
Share on other sites

have you tried an explicit cast like
[color="#000088"]if [color="#666600"]([color="#660066"]Input[color="#666600"].[color="#660066"]GetKeyDown[color="#666600"](([color="#000000"]skills[color="#666600"][[color="#000000"]i[color="#666600"]] as skill).[color="#660066"]Hotkey[color="#666600"]))

also, might need to declare array as
var Arrayname: Array = new Array();

Regs,
Mike
@runonthespot


This solved the problem. Thanks!

Share this post


Link to post
Share on other sites
Hidden
To be honest, if you're working on iphone, and forced to use #pragma stict anyway, I'd strongly recommend moving to C# - because the recasting is far less onerous and there's a lot more information on C# like that. What makes C# more difficult that Javascript normally is that you're forced to decide on data type and don't get to take advantage of UnityScript's implicit casting. Since that advantage is gone with pragma strict, rather use the richer language.

That way you get the use of C# data structures like List.

Your code, in c# could look like this (not too scary right?)

[color=#000088]using System.Collections.Generic; [color=#000088]public[color=#000000] [color="#000088"]List<Skill>[color=#000000] skills [color=#666600]=[color=#000000] [color=#000088]new[color=#000000] [color="#660066"]List<Skill>()[color=#666600];

[color="#1c2837"]

[color=#000000]
[color=#000088]class[color=#000000] [color=#660066]Skill[color=#000000]
[color=#666600]{[color=#000000]
String[color=#000000] [color=#660066]Hotkey; String dub[color=#666600]; Script [color=#660066]Skills[color=#666600];[color=#000000]
[color=#666600]}<br style="color: rgb(28, 40, 55); font-size: 13px; line-height: 16px; text-align: left; background-color: rgb(250, 251, 252); "><br style="color: rgb(28, 40, 55); font-size: 13px; line-height: 16px; text-align: left; background-color: rgb(250, 251, 252); ">[color=#000088]if[color=#000000] [color=#666600]([color=#660066]Input[color=#666600].[color=#660066]GetKeyDown[color=#666600]([color=#000000]skills[color=#666600][[color=#000000]i[color=#666600]].[color=#660066]Hotkey[color=#666600]))

Share this post


Link to post

[quote name='runonthespot' timestamp='1321057254' post='4883090']
have you tried an explicit cast like
[color="#000088"]if [color="#666600"]([color="#660066"]Input[color="#666600"].[color="#660066"]GetKeyDown[color="#666600"](([color="#000000"]skills[color="#666600"][[color="#000000"]i[color="#666600"]] as skill).[color="#660066"]Hotkey[color="#666600"]))

also, might need to declare array as
var Arrayname: Array = new Array();

Regs,
Mike
@runonthespot


This solved the problem. Thanks!
[/quote]




No problem. Given you're using #pragma strict, you might as well switch to C# - there are virtually no other differences besides being forced to be explict with types, and since it's required for all C#, you don't have to go through the pain of rejigging any js scripts you pick up from the net.

Also, I strongly recommend posting questions like this to answers.Unity3d.com or the Unity forums- lots more people around to answer Unity specific questions, rather than relying on people like me spotting it on a random internet search ;-)

Best,
Mike

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!