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 HunterAMG · Feb 03, 2014 at 03:13 AM · prefabvariableaccessanother

Cannot access a variable in another Script.

How can I access a variable(bool) in script B from script A, if script A is in a prefab, and script B is not. I just need a general example.

 //This is Script A attached to a prefab
     void Update(){
     if (whatever == true) {
     //Blabla
             }
         }
     
     
     
     //This is Script B attached to a gameObject in Scene
     public bool whatever;
     public void OnGUI(){
     if(Gui.Button(new Rect(1,3,3,7))){
         whatever=true;
         }
     }
 

I have tried using the inspector but it isnt possible because A is attached to a prefab. So I go like:

 //This is Script A attached to a prefab
 void Update(){
 if (ScriptB.whatever == true) {
 //Blabla
         }
     }
 
 
 
 //This is Script B attached to a gameObject in Scene
 public bool whatever;
 public void OnGUI(){
 if(Gui.Button(new Rect(1,3,3,7))){
     whatever=true;
     }
 }

But then the compiler yells: "An object reference is required for the non-static field, method, or property ScriptB.whatever" and I try to change it to static bool whatever;

and the compiler goes: "Bro, I cannot access that variable due to its protection level"

I have tried a lot of different things but none of them have worked, What do!?!?

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
0
Best Answer

Answer by robertbu · Feb 03, 2014 at 03:23 AM

Your question is backwards. Since 'whatever' is declared in ScriptB, you are trying to have Script A access 'whatever' in Script B. So assuming ScriptA is the prefab, you can do this. At the top of the file put:

 private ScriptB scriptB;  // Change to whatever the script is named

Then in Start():

 void Start() {
     scriptB = GameObject.Find("GONameForScriptB").GetComponent<ScriptB>();
     // The Find() uses the name of the game object with ScriptB.
 }

Then in Update() you would do:

 if (scriptB.whatever) {
     //Do something
 }

 

As the code stands now, you need to be sure the named game object with ScriptB exists since you will get a null reference exception from this code if it does not.

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 JotaRata · Feb 03, 2014 at 03:57 AM

or you may declare that bool, at begining of the class

     //Your Class ScriptA
     public class Script : MonoBehaviour{
         // declare variables
      public bool Whatever;
      public int NumberOfTheBeast=666;
     
        void Start(){}
 
      void Update(){
 
    //do Something
 }
 }


and then...

 //script B
 
   public class blabla: monobehaviour{
 
   public/private bool scriptABool;
   public/private Gameobject/Object scriptAObject;
 
 void Start(){
 
  scriptABool=scriptAObject.GetComponent<scriptA>().whatever;
 
 if (scriptABool==true)
 {
    /Do Stuff:D
 
 }
 }
 }
Comment
Add comment · Show 2 · 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 robertbu · Feb 03, 2014 at 04:18 AM 0
Share

@JotaRata - this won't work. According to the OP, ScriptA is on a prefab, and therefore it won't exist when the app starts...plus you will not be able to initialize 'scriptAObject' in the Inspector. The result is that line 10 in the second script will fail with a null reference exception..

avatar image JotaRata · Feb 04, 2014 at 02:44 AM 0
Share

robertbu

ooh you're right, only will work if ScriptA is on a GameObject, well thanks for your comment i've learned a new thing about unity:)

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

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

Related Questions

Can't access variable from another script. 1 Answer

My script accessing a variable from another script recieves an error 1 Answer

Accessing a variable effected by a GUI slider from another scipt 1 Answer

How Do I Modify This Variable in Another Script? 1 Answer

Prefabs and variables 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