• 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
0
Question by sanks007 · Sep 11, 2013 at 05:56 AM · prefabvariables

Accessing variable defined in a Script attached to Prefab

I have a public variable defined as public var ElementJson:SimpleJSON.JSONClass; on a script named ElementInfo which is attached to a prefab Element.I am using Simple JSON Parser for storing element information in JSON.Suppose there are 10 instance of Element i.e Element1-Element10. And ElementInfo is attached to each of the instance. Now my question is how to access ElementJson of each instance in a script that is attached to a empty GameObject.This script is used for accessing overall application information i.e one can say a controller script which will handle entire application data and pass it to other components.!

Comment
Add comment · Show 12
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 DannyB · Sep 11, 2013 at 06:04 AM 0
Share

GameObject.GetComponent

avatar image sanks007 · Sep 11, 2013 at 06:12 AM 0
Share

I tried doing this

 var elementInfo:ElementInfo;
 function Start () 
 {
     elementInfo=FindObjectOfType(typeof(ElementInfo));
 }
 function Update () 
 {
     print(elementInfo.Element.ToString());
 }

But it shows

   NullReferenceException: Object reference not set to an instance of an object
     TestScriptForJson.Update () (at Assets/ReactionScreenScript/TestScriptForJson.js:30)

can you point out what is the problem. I have been using this method successfully for accessing variables of other scripts. And just one more question for understanding purpose. Since there are multiple instances of prefab so when accessing the variable how will it come to know which element it is accessing or it will take information from all the instances.

avatar image vexe · Sep 11, 2013 at 06:19 AM 0
Share

That's not how your properly get components from other game objects. FindObjectOfType of used to find game objects, not to get a component. That's what GetComponent is there for. Get the component, make sure it's not null and then use it to fetch any variable you want.

As per your query on whether FindObjectOfType gets the first or all instances, from this link: "Returns the first active loaded object of Type type. Please note that this function is very slow. It is not recommended to use this function every frame. In most cases you can use the singleton pattern ins$$anonymous$$d."

avatar image DannyB · Sep 11, 2013 at 06:22 AM 0
Share

FindObjectOfType returns the first one
FindObjectsOfType returns a list

avatar image vexe · Sep 11, 2013 at 06:39 AM 0
Share

Glad you go it sorted out! Convert your last comment to an answer and accept it to close this question :)

avatar image sanks007 · Sep 11, 2013 at 06:45 AM 0
Share

Just got one more thought regarding FindObjectOfType and FindObjectsOfType you guys said that FindObjectOfType will fetch me only the first active instance of that script. So If am looking to access the script which is attached to a prefab it will get me just the first instance of the script and not the others. ?

avatar image vexe · Sep 11, 2013 at 06:51 AM 1
Share

Again, you're still confusing between 'finding an object' and 'getting a component/script from an object' - FindObjectOfType will search the whole scene and once it finds the first active object of the type you specify it will return it to you. While if you had a game object and wanna get a component from it, you should do gameObject.GetComponent

avatar image sanks007 · Sep 11, 2013 at 06:58 AM 0
Share

Ohkkk got it. Sorry for all the confusion i am quite new to unity and trying to learn as much as i can and get confuse in the end.

avatar image DannyB · Sep 11, 2013 at 07:08 AM 1
Share

I strongly suggest you try to avoid all those FindObject variations. In some cases you have no choice, and this is a good option, but you should familiarize yourself with other mechanisms for obtaining a reference to an object. I have developed two full games now, and never used FindObjectOfType or its array returning brother.

avatar image sanks007 · Sep 11, 2013 at 07:14 AM 0
Share

Using it for scripts and not for GameObjects. $$anonymous$$ay be somewhere made some mistake but since its working with this would stick to them and get done with the application.And may be later change it after releasing beta version of the application.Thanks for your helps its nice to get help from experienced persons like you..! If Can you check this link and provide your valuable suggestion on this application which am working on as of now.!

avatar image vexe · Sep 11, 2013 at 07:30 AM 0
Share

Doing something in a way that 'works' doesn't necessarily mean that's the best way to do it. You should always seek to learn the best habit of doing something. If you learn a wrong habit it will stick with you and the quality of your apps will degrade. And the best way to learn good habits, is to learn from those of higher experience. UnityGems is a good place to learn with high quality - Also, I advice you to learn C#, it has a lot of powerful features, check this out.

avatar image sanks007 · Sep 11, 2013 at 07:37 AM 0
Share

True.. its not the right way but then its better to launch a beta version first and then work on this bad aspect later and make it better for user.And I will definitely learn from the UnityGems. As of now i am sticking to Unity Script as am comfortable with it though will learn C# side by side. Learning new tricks is always good and always eager to learn them.

2 Replies

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

Answer by vexe · Sep 11, 2013 at 06:10 AM

You just need a call to GetComponent from the script you're trying to access the component SimpleJSON from. More detailed information on getting components can be found here and here.

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
avatar image
0

Answer by sanks007 · Sep 11, 2013 at 06:37 AM

Thanks both of you..! this is what I did and it worked..

 var Element:SimpleJSON.JSONClass;
 var elementInfo:ElementInfo;
 function Update () 
 {
     var gameobject : GameObject;
     for(gameobject in testscript.elementarrays)
     {
         elementInfo=gameobject.GetComponent("ElementInfo");
         Element=elementInfo.Element;
         print(Element.ToString());
     }
 }

Since the instances of Element are created at runtime kept the code in the update method and now am able to access it. And thanks guys once again..! @vexe FindObjectOfType i am calling this function in start method and not in update.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is there a way to set components in a prefab before it is Instantiated? 0 Answers

Can one use of a static variable, change it globaly? 2 Answers

Showing GUI by pressing diffrent buttons? 1 Answer

How can I take a variable from another script and then apply it to a newly insantiated prefab? 2 Answers


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