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 Fede · Jun 21, 2010 at 02:21 PM · instantiateprefabiphone

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

Comment
Add comment
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

2 Replies

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

Answer by StephanK · Jun 21, 2010 at 02:43 PM

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.

Comment
Add comment · Show 5 · 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 Fede · Jun 21, 2010 at 03:02 PM 0
Share

and how can i do that? thanks in advance, Fede

avatar image Cyclops · Jun 21, 2010 at 03:26 PM 0
Share

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

avatar image Fede · Jun 22, 2010 at 07:33 AM 0
Share

if it was (GameObject)Instantiate, it does not work unfortunately. But perhaps i misunderstood... Thanks a lot for your answers, Fede

avatar image StephanK · Jun 22, 2010 at 08:30 AM 0
Share

I checked it and updated my answer.

avatar image Fede · Jun 22, 2010 at 08:59 AM 0
Share

Thanks a lot! It works perfectly! Thank you for answering me, and sorry for the time i made you lose ^_^

Cheers, Fede

avatar image
0

Answer by Cyclops · Jun 21, 2010 at 02:44 PM

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.

Comment
Add comment · Show 6 · 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 Fede · Jun 21, 2010 at 03:03 PM 0
Share

nope: it says that "position is not a member of UnityEngine.Object" Fede

avatar image Cyclops · Jun 21, 2010 at 03:18 PM 0
Share

Weird... I decided to test the code and started by cutting/pasting your example - which worked perfectly in my project. Researching...

avatar image Fede · Jun 21, 2010 at 03:19 PM 0
Share

weird indeed. I am using unity iPhone 1.5...

avatar image Cyclops · Jun 21, 2010 at 03:23 PM 0
Share

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.

avatar image Cyclops · Jun 21, 2010 at 03:24 PM 0
Share

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

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

No one has followed this question yet.

Related Questions

i want to assign different colors to rows or different colors to the brick? 0 Answers

Instantiated objects always at position 0,0,0 0 Answers

Destroying Objects. 1 Answer

How do i Instantiate a prefab with specific assests included 3 Answers

Instantiate in Editor 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