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

Show more comments

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.

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

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

Is there a way to set components in a prefab before it is Instantiated? 0 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