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 Galaroth · Apr 13, 2014 at 08:12 PM · getcomponentaccess

How do i access a variable from another script?

Hi. I want to check an int called buff, from another script (Player script). What i want is: when the buff=1, i want it to change my buffOn int to 1, which is placed in a different script than the player script. Right now i get this erroe "NullReferenceException: Object reference not set to an instance of an object ToughBox.Update () (at Assets/ToughBox.cs:17)"

My script looks like this:

using UnityEngine; using System.Collections;

public class ToughBox : MonoBehaviour { public int buffOn;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {    
     Player Pscript = GetComponent<Player>();
     if (Pscript.buff == 1)
     {
         buffOn = 1;
     }
 }
Comment
Add comment · Show 2
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 Lachee1 · Apr 13, 2014 at 08:23 PM 0
Share

the object that this script is on, does it have the Player script attached too? Also it says the error is on line 17, but you have only supplied up to line 13.

avatar image Galaroth · Apr 13, 2014 at 09:05 PM 0
Share

The object this script is on, does NOT have the Player script attached to it. Don't know why it displays the lines like this, but in my script line 17 is line 9 (if (Pscript.buff == 1))

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by akauper · Apr 13, 2014 at 08:36 PM

The error is letting you know that Pscript is not correctly referenced to your instanced script. In other words, your GetComponent() method is not working.

For GetComponent to work both scripts must be attached to the same game object. Is this the case?

Also, there is rarely a need to call GetComponent from within Update (it slows things down). Assign your Pscript variable within Start or Awake instead.

Comment
Add comment · Show 4 · 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 Galaroth · Apr 13, 2014 at 09:07 PM 0
Share

No the Player script is not attached to the object which this script is on.

But if i set GetComponent in Awake will it notice if the int from the Player scrip changes?

avatar image akauper · Apr 14, 2014 at 01:13 AM 0
Share

Yes. What you are 'getting' is a reference to that script not a duplication of its current state. Any time you use dot notation to access the script it will access its current state.

But, more importantly, if both scripts are not on the same gameobject, then GetComponent wont work. First you need to find which gameobject the script is on, then use GetComponent.

For example,

 gameObject.Find("nameofgameobject").GetComponent<Player>();

Simply using GetComponent() asks unity to find the player script attached to the same gameobject as the script which is calling GetComponent. GetComponent does not search for all scripts within any object in the scene.

avatar image Galaroth · Apr 14, 2014 at 10:03 AM 0
Share

Ok thanks for $$anonymous$$ching me that. However i still get an error. even though i find the Player gameobject and the Player script attached to it in Start ();

NullReferenceException: Object reference not set to an instance of an object ToughBox.Start () (at Assets/ToughBox.cs:14)

using UnityEngine; using System.Collections;

public class ToughBox : $$anonymous$$onoBehaviour { public int buffOn;

 // Use this for initialization
 void Start () {
     GameObject.Find("Player").GetComponent<Player>();
     Player Pscript = GetComponent<Player>();
     Pscript.buff = buffOn;
 
 }

(btw line 14 is the Pscript.buff = buffOn; part.)

avatar image akauper · Apr 15, 2014 at 09:21 PM 0
Share
 // Use this for initialization
 void Start () {
     Player Pscript = GameObject.Find("Player").GetComponent<Player>();
     Pscript.buff = buffOn;
  
 }

Your first line under start is just declaring "find the gameobject and get the component on it... oh you have it? ok dont assign it to anything just load it into memory" then when your start function ends it is unloaded from memory.

The 2nd line then asks unity to Get the component Player on the current gameobject (on which it doesnt exist).

See my code above for the correct syntax

avatar image
0

Answer by uanmanarmy · Apr 15, 2014 at 10:47 PM

 public GameObject player; //attach the object with the script.
 public NameofTheScript variable;
     
         void Awake () {
             variable = player.GetComponent<NameofTheScript> ();
         }
         
         // Update is called once per frame
         void Update () 
         {
           variable.variablefromNameofTheScript;
         }
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

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

Related Questions

Enable My Script on Spawn 0 Answers

Accessing variables from a different gameobject 1 Answer

How to get script from Object with (Clone)'s script? 2 Answers

how do you access booleans from another script 1 Answer

Why are these scripts getting different icons and could it have anything to do with my problem? 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