x


Accessing prefab instances at runtime (unity iphone)

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:

var prefab: Transform;

function Start () {
    var instance  = Instantiate (prefab, Vector3 (0,10,0), Quaternion.identity);
    instance.transform.position.x = 10;
}   

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:

 'transform' is not a member of 'UnityEngine.Object'. 

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 ^_^

more ▼

asked Jun 21 '10 at 02:21 PM

Fede gravatar image

Fede
31 3 3 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;

function Start () {
  var go : GameObject = GameObject.Instantiate(prefab);
  Debug.Log(go.transform.position.x);
}

The point is that JavaScript will do the casting for you, but for this to work you will have to "type" the variable.

more ▼

answered Jun 21 '10 at 02:43 PM

StephanK gravatar image

StephanK
6k 40 53 93

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)
10|3000 characters needed characters left

Change the code to this:

instance.position.x = 10;

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.

more ▼

answered Jun 21 '10 at 02:44 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

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)
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:

x3892
x2014
x1726
x1288

asked: Jun 21 '10 at 02:21 PM

Seen: 2740 times

Last Updated: Jun 22 '10 at 07:34 AM