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 Olie · Nov 10, 2010 at 01:38 AM · errorinstantiatevariablemissingfieldexception

Can't access prefab's variables, once instantiated.

I'm trying to instantiate a game object, then set its variables before it runs Start(). So I have code like this:

var myPrefab : GameObject;

function Start() { types = BlockTypes.GetValues(BlockTypes); var xfrm : Transform = transform; // xfrm.Translate(xx, yy, zz), etc... oneBlox = Instantiate (myPrefab, xfrm.position, transform.rotation); oneBlox.theBlockType = someIntValue; oneBlox.transform.parent = this.transform; }

The problem I'm having is that this line:

oneBlox.theBlockType = someIntValue;

is generating an error at runtime:

MissingFieldException: Field 'UnityEngine.Transform.theBlockType' not found.
[stack trace snipped]

What's the correct syntax to (a) instantiate an object of type MyPrefab, then (b) set one of the prefab's variables?

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 Olie · Nov 10, 2010 at 06:05 AM

Thanks, Eric5h5. Your answer was technically correct but, as a n00b, I found it frustrating that so many answers on this site talk about the theory but not the actual code, so I'm going to give the answer I think that others with this question will want. Here's the working code:

var iblxPrefab : GameObject;

function Start() { var oneBlox : GameObject; oneBlox = Instantiate (iblxPrefab, xfrm.position, transform.rotation);

 var bloxScript = oneBlox.GetComponent("blxScript"); // name of script without the ".js" at the end.
 bloxScript.theBlockType = ii;                       // name of variable in script.

}

Key bits: (a) to instantiate a prefab, the syntax is:

    var oneBlox : GameObject;
    oneBlox = Instantiate (iblxPrefab, xfrm.position, transform.rotation);

(for some reason, I was unable to get the all-in-one-line version of this to work, but that may be me, rather than a Unity limitation) and (b) to access an object's script's variables, the syntax is this:

    var bloxScript = oneBlox.GetComponent("blxScript"); // name of script without the ".js" at the end.
    bloxScript.theBlockType = ii;                       // name of variable in script.
Comment
Add comment · Show 3 · 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 Tetrad · Nov 14, 2010 at 07:45 AM 0
Share

You should mark this answer as "accepted" so the auto-bump bot doesn't bump this question again.

avatar image Olie · Nov 14, 2010 at 09:26 PM 0
Share

Done. Thanks, Tetrad.

avatar image dissidently · Dec 27, 2010 at 09:12 PM 0
Share

sorry, retard here. Can you make this a little clearer with regards the variables in scripts. Which variable is inside which script? var theBlockType is in script "blxScript" or is ii in blxScript?

avatar image
3

Answer by Eric5h5 · Nov 10, 2010 at 02:47 AM

You need to use GetComponent. Variables are not properties of GameObjects or Transforms, they are properties of scripts.

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

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

Variables defined in editor, but clear on runtime 2 Answers

GUID variable declaration error 1 Answer

error CS0841: A local variable `holdSound' cannot be used before it is declared 1 Answer

Simultaneous Null Reference Exception and expected value 1 Answer

Missing Field Exception 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