- Home /
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);
}
}
Too much useless code posted here. Don't ever use an Array.
"Don't ever use an Array". Should I take that with a grain of salt? Or do you mean it quite literally?
Literally. You probably want List. ins$$anonymous$$d. 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.
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.
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.
Answer by Drakestar · May 08, 2012 at 05:18 PM
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
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.
I got it to work, and I also defined the type of the created object
var newPlanet:GameObject = Instantiate(clonePlanet,planetPosition,Quaternion.identity);
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.
That's because Array stores Object, not what it needs to. It has nothing to do with JavaScript.
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#. ($$anonymous$$eep in $$anonymous$$d 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.