|
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.
(comments are locked)
|
|
Try explicitly casting back to a GameObject, like this: 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 -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)
|

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. 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.
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.