• Register

Sound designer/Game Developer based in South Yorkshire, UK

Latest Media

We're sorry, but no images, videos or audio files have been added to this gallery.

If you would like to share media with the community, sign up and you can.

Blog RSS Feed Report abuse Latest Blog: Unity - javascript built in Arrays of instances of classes

0 comments by TommyGoogle on Aug 18th, 2012

edit 3 - just deleted this whole post in one click that was misdirected by stupid android web browser.. but no confirmation from desura?? not good!!

was still in laptop's history.. so i got it back..

anyway - my fix worked! and got other stuff working in android..

gonna have a belter of a thing soon!!

:)


any ideas about this:-

javascript code:
#pragma strict

function Start () {

var arrayofclass:iamaclass[] = new iamaclass[2];

}


class iamaclass
{

  function iamaclass()
  {
  Debug.Log("i am not happenning - no constructors with arrays??");
  }

} 

??

is it even possible to set multiple constructors at once for an array of classes, or do i need a for loop to do this manually after each time that i want to do it?? is that how it should be done??

edit:-
Turns out that constructors are run for each instance in an array of class instances, but only if they don't want anything in the brackets..

javascript code:
#pragma strict

var arrayofclass:iamaclass[];

function Start () {

arrayofclass = new iamaclass[2];

}


class iamaclass
{

var hooter:int;

var dooter:int;

  function iamaclass(passed:int)
  {
  hooter = passed;
  dooter = 34;
  }

}

in this example, the constructor won't be run!

javascript code:
#pragma strict

var arrayofclass:iamaclass[];

function Start () {

arrayofclass = new iamaclass[2];

}


class iamaclass
{

var spooter:int;

var dooter:int;

  function iamaclass()
  {
  var passed:int = 33;
  spooter = passed;
  dooter = 34;
  }

}

but here, it will!!

edit 2- got some valuable tips from here:-
Answers.unity3d.com
regarding instantiation of built in arrays of classes

if you are going to do:-
var personArray : Person[] = new Person[700];

and then instantly attempt to access
personArray[352].name
or similar, you'll get an error...this object doesn't exist!! (though accessing it "a while" later will work fine!?!?!)
what is needed, as stated at the above address:-

"You need to instantiate a Person object for each element of the newly-declared array. The easiest way to do this would be in a for loop that iterates through the array, calling personArray[i]= new Person(); "

(cheers noony!)

if you DON'T do this, however, it seems that unity's explorer WILL automatically instantiate person objects for this array if you run your code within the unity environment, though it will take a while (seemingly 2 update frames every time on my pc..) for this to occur.. potentially leading to some very hard to spot bugs, and almost certainly leading to code that works fine in the unity environment thingy (what's the actual word for this!?!) but won't run when compiled for anything else!!

at least i think that this is what is happening.. i'm gonna see if altering this fixes my thing that isn't working anywhere except within unity...

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Level
Avatar
Avatar
Offline Since
Mar 28, 2013
Country
United Kingdom United Kingdom
Gender
Male
Age
33
Member Watch
Track this member
Statistics
Activity Points
20
Rank
48,420 of 326,369
Watchers
0 members
Time Online
1 second
Comments
1
Site Visits
69
Profile Visitors
1,356 (2 today)
Contact
Private Message
Send Now
Email
Members Only

No one is friends with TommyGoogle awww. You should invite your mates to the site, or make new ones.