Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by simeonradivoev · May 08, 2012 at 03:52 PM · gameobjecttransformarray

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);
      }
  }
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · May 08, 2012 at 04:17 PM 1
Share

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

avatar image FLASHDENMARK · May 08, 2012 at 05:00 PM 0
Share

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

avatar image Jessy · May 08, 2012 at 05:05 PM 0
Share

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.

avatar image FLASHDENMARK · May 08, 2012 at 05:27 PM 0
Share

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.

avatar image Eric5h5 · May 08, 2012 at 07:21 PM 3
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · May 08, 2012 at 06:07 PM 1
Share
  • 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.

avatar image simeonradivoev · May 08, 2012 at 06:17 PM 0
Share

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

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

avatar image Drakestar · May 08, 2012 at 06:33 PM 0
Share

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.

avatar image Jessy · May 08, 2012 at 06:58 PM 0
Share

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

avatar image Drakestar · May 08, 2012 at 08:59 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Variables Transform vs GameObject 1 Answer

Getting the transform from a GameObject list 3 Answers

Keep adding targets to a list 2 Answers

How do I check multiple gameObjects transform positions? 3 Answers

Instantiating from an object that's in an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges