|
Hello everybody! I have been struggling a lot with this problem: i cannot dinamically access instantiated prefab. The following is the code i am using:
the script seems pretty straightforward: i have assigned it to the main scene camera, and i have dragged the desired prefab to the "prefab" variable in the inspector. I should be instantiating an "instance" clone of "prefab": yet i seem unable to accomplish this task, as adding the line "instance.transform.position.x=10" returns the following error:
i assume i should declare the variable type of instance (like var instance : GameObject = ..., wich does not work). Any idea? can you point me the right direction? Thanks ^_^
(comments are locked)
|
|
Instantiate returns a UnityEngine.Object, which is not the same as a GameObject, so you will have to cast the returned Object to a GameObject before you can use its transform component. After testing this in Unity iphone I realized that it seems to work differently from the normal unity version. To get what you want you have to this: var prefab : Transform;
The point is that JavaScript will do the casting for you, but for this to work you will have to "type" the variable. and how can i do that? thanks in advance, Fede
Jun 21 '10 at 03:02 PM
Fede
@Fede, one way is to prefix the Instantiate with (GameObject), note the parentheses. But if the error is a compile-time one, I'm not sure if casting will work. Worth a try though.
Jun 21 '10 at 03:26 PM
Cyclops
if it was (GameObject)Instantiate, it does not work unfortunately. But perhaps i misunderstood... Thanks a lot for your answers, Fede
Jun 22 '10 at 07:33 AM
Fede
I checked it and updated my answer.
Jun 22 '10 at 08:30 AM
StephanK
Thanks a lot! It works perfectly! Thank you for answering me, and sorry for the time i made you lose ^_^ Cheers, Fede
Jun 22 '10 at 08:59 AM
Fede
(comments are locked)
|
|
Change the code to this:
You declared and Instantiated instance, already as a Transform, so you wouldn't refer to its transform component. If you had declared instance as a GameObject, then you would refer to transform. Update: Instantiate can be used on Transforms and other derivatives from Object, and it returns whatever type you cloned. nope: it says that "position is not a member of UnityEngine.Object" Fede
Jun 21 '10 at 03:03 PM
Fede
Weird... I decided to test the code and started by cutting/pasting your example - which worked perfectly in my project. Researching...
Jun 21 '10 at 03:18 PM
Cyclops
weird indeed. I am using unity iPhone 1.5...
Jun 21 '10 at 03:19 PM
Fede
Hm, I don't know then, sounds like iPhone is different. I was about to suggest you add a debugging line, checking for instance being null, but your original error is a compile-time error, right? Being null would be a run-time error.
Jun 21 '10 at 03:23 PM
Cyclops
Also - might update your original Question to indicate it's iPhone, that appears to be relevant information (since the code works fine on my PC).
Jun 21 '10 at 03:24 PM
Cyclops
(comments are locked)
|
