x


Array - 'transform' is not member of Object

it gives me an error 'transform' is not member of Object. How do I tell unity that the objects I put in the array , are GameObjects.

#pragma strict


var MaxPlanets:int = 10;
var clonePlanet : GameObject;
var PlanetMaterial : Material;
var planetGroundMaps : Texture[];
var planets:Array = new Array();

function Start () {
createPlanets();
}

function Update () {
rotatePlanets();
}


function createPlanets():void{
var radiusMax:float = 5;
var radiusMin:float = 1;
var radiusRan:float;
var Angle:float;
var planetPosition:Vector3 = new Vector3(0,0,0);
var planetRotation:float;
var RanTexture:int;


    for(var i:int = 0;i <= MaxPlanets;i++){
       radiusRan = Random.Range(radiusMin,radiusMax);;
       Angle = ((360 / MaxPlanets) * i) * (Mathf.PI/180);
       planetPosition.x = Mathf.Cos(Angle) * radiusRan;
       planetPosition.z = Mathf.Sin(Angle) * radiusRan;
       var newPlanet = Instantiate(clonePlanet,planetPosition,Quaternion.identity);
       RanTexture = Random.Range(0,4);
       newPlanet.renderer.material = PlanetMaterial;
       newPlanet.renderer.material.SetTexture("_PlanetGround",planetGroundMaps[RanTexture]);
        if(RanTexture<3){
        newPlanet.renderer.material.SetTexture("_Clouds",null);
        newPlanet.renderer.material.SetTexture("_HeightMap",null);
        newPlanet.renderer.material.SetTexture("_lights",null);
       planets.Add(newPlanet.gameObject );
        }
    }
}

function rotatePlanets():void{
    for (var i in planets){
       i.transform.RotateAround(Vector3.zero,Vector3.up,Time.deltaTime);
    }
}
more ▼

asked May 08 '12 at 03:52 PM

simeonradivoev gravatar image

simeonradivoev
37 6 8 9

Too much useless code posted here. Don't ever use an Array.

May 08 '12 at 04:17 PM Jessy

"Don't ever use an Array". Should I take that with a grain of salt? Or do you mean it quite literally?

May 08 '12 at 05:00 PM OrangeLightning

Literally. You probably want List. instead. That doesn't render right and I have no idea how to make it. After the period after List, there is a less than sign, then Transform, then a greater than sign.

May 08 '12 at 05:05 PM Jessy

Okay, but why should we favor Lists above Arrays? What do they do that Arrays don't? I use Arrays all the time and I have never had a problem using Arrays.

May 08 '12 at 05:27 PM OrangeLightning

Jessy is correct, don't ever use Array. There is absolutely nothing that can be done with Array that can't be done better with List. And folks, remember the difference between the JS Array class and arrays. JS Array class = bad, arrays = good.

May 08 '12 at 07:21 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try explicitly casting back to a GameObject, like this:

for (var i : GameObject in planets)

No access to Unity right now so I can't verify, but it should work.

Edit: also see this answer: [http://answers.unity3d.com/questions/17865/classes-and-type-casting.html][1] [1]: http://answers.unity3d.com/questions/17865/classes-and-type-casting.html

more ▼

answered May 08 '12 at 05:18 PM

Drakestar gravatar image

Drakestar
916 6 7 14

-1 for not knowing what #pragma strict is. Definitely switch to C#, but know why you did, so you don't end up being one of these guys who thinks that UnityScript and C# both have merits.

May 08 '12 at 06:07 PM Jessy

I got it to work, and I also defined the type of the created object

var newPlanet:GameObject = Instantiate(clonePlanet,planetPosition,Quaternion.identity);

May 08 '12 at 06:17 PM simeonradivoev

The original code used #pragma strict, but that didn't prevent the type-conversion issue that triggered the question. Simon, glad you got this to work.

May 08 '12 at 06:33 PM Drakestar

That's because Array stores Object, not what it needs to. It has nothing to do with JavaScript.

May 08 '12 at 06:58 PM Jessy

From everything I've seen, UnityScript is weakly typed. It's statically typed for behind-the-scenes optimizations, but that is not the same as strong typing. Per the docs, the language falls back to dynamic typing when the type cannot be inferred at compile-time, and you can force compile-time errors on type mismatches via #pragma strict.

But that's not what the second part of the answer was refering to. I was commenting on the fact that the original poster seemed to have run into a situation where static compile-time typing had failed, dynamic typing took over, and that dynamic type inference was failing during the runtime - which could only be solved via an explicit cast. This chain of events cannot happen in C#. (Keep in mind that I'm writing all of this without access to testing in Unity, it's all educated theory.)

Either way I've removed that part of the answer because it doesn't concern the actual answer, and goes into CS semantics that confuse rather than help.

May 08 '12 at 08:59 PM Drakestar
(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:

x2171
x1396
x1332
x699

asked: May 08 '12 at 03:52 PM

Seen: 1097 times

Last Updated: May 09 '12 at 01:44 PM