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 SirMalarkey · Jan 31, 2013 at 10:42 PM · gameobjectvariables

How to access variables from a different script on a different GameObject?

I have a game object Called PistolAmmo with a script called AmmoPistol.js attached to it, i also have the player game object with a script called WeaponManagerScript.js(this script manages the ammo count for all weapons), i want AmmoPistol.js to be able to access and modify variables inside of WeaponManagerScrit.js. I have been trying for the past eighteen hours to get this to work but the closet i have gotten is this.

 var weaponManager : WeaponManagerScript;
 
 function Start ()
 {
     weaponManager = gameObject.GetComponent("WeaponManagerScrip");
     Debug.Log ("Hello");
 }
 
  function OnTriggerEnter (collider : Collider) 
 {    
     Debug.Log ("Something is flipping my trigger!!!!");
     if(collider.gameObject.FindWithTag("Player"))
         {
         Debug.Log ("Oh babay you Just Flipped My Trigger!!!");
         if (weaponManager.ammoCountPistol <= 48)
             {
                 Debug.Log ("Its Dangerous Ahead You'll Need This!");
                 weaponManager.ammoCountPistol += 8;
                 Destroy(this.gameObject);
             }    
         }
 }

The above code gives me this error.

 NullReferenceException: Object reference not set to an instance of an object
 PistolAmmo.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/WeaponScripts/PistolAmmo.js:17)


If anyone knows a way to do this or fix any current problems in my script i would appreciate the help, 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
0
Best Answer

Answer by mStep · Jan 31, 2013 at 10:51 PM

This line:

 weaponManager = gameObject.GetComponent("WeaponManagerScrip");

is spelled wrong, I believe. Unless your script is actually called "WeaponManagerScrip". But more importantly, you haven't referenced the gameObject that WeaponManagerScript.js is attached to. You've just used the generic "gameObject", which means you are trying to get the instance of WeaponManagerScript.js that is attached to the PistolAmmo object. If you wanted to get the instance of WeaponManagerScript.js that is on a different gameObject, you would need to either reference it directly through a public variable, or find it, like this:

 weaponManager = GameObject.Find ("TheObjectWIthWeapnManager").GetComponent(WeaponManagerScript);

You also need to make sure the variable you're trying to access is public, and not protected.

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 SirMalarkey · Jan 31, 2013 at 11:00 PM 0
Share

Sweet mother of Iverseen, I have been staring at my monitor for sooooo long trying to fix this my eyes have turned completely red, and the fix was so darn simple. Thank you so much, if i could up vote you, i would.

avatar image mStep · Jan 31, 2013 at 11:57 PM 0
Share

Not a problem. Sometimes the simplest answers are the hardest to find.

avatar image
0

Answer by nickseegobin · Jan 31, 2013 at 11:52 PM

STATIC VARS

Set the variables as "static";

firstScript.js:

 public static var score:int = 2;


secondScript.js:

 //prints out the value in the console
 Debug.Log(firstScript.score);
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

11 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

Related Questions

Getting errors when referencing a variable. 1 Answer

Public variable in script different for every game object. 1 Answer

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

How can I set the order of destruction of game objects? 1 Answer

How to save a gameObjects on a prefab and variables with a script? 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