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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Jonster · Mar 09, 2013 at 01:12 AM · variablenullreferenceexceptionaccessing

accessing a variable from another script.

I am very new to programming and struggling with a problem. I am trying to access the variable "score" from one script and use "score" to determine the height of a guitexture on another script. I have tried it a couple ways but i keep getting this error: Null Reference Exception: Object reference not set to an instance of an object.
This is how my script is written.

//this is my first script, which is named: stonyScript

var score : float = 0;

function OnCollisionEnter(col : Collision){

 if(col.gameObject.name == ("asteroid")){
     score += 10;
 }


//here is the second script.

currentHeight = stonyScript.js.score;

function Update(){

guiTexture.pixelInset.width = 20;

guiTexture.pixelInset.height = 10 + currentHeight;

}

Comment
Add comment · Show 3
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 landon912 · Mar 09, 2013 at 01:28 AM 0
Share

fix the format of your code please. :D

avatar image hoy_smallfry · Mar 09, 2013 at 02:50 AM 0
Share

Possible duplicate: http://answers.unity3d.com/questions/407951/how-do-i-make-my-score-go-up-upon-a-collision.html

avatar image Jonster · Mar 09, 2013 at 03:22 AM 0
Share

Thank you jbarba_ballytech. I was able to fix my problem thanks to your reference :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by landon912 · Mar 09, 2013 at 01:30 AM

You have to tell it where to get stonyScript from.

 stonyScript = GameObject.Find("Insert the name of the object that stonyScript is located").GetComponent(stonyScript);
 currentHeight = stonyScript.score
Comment
Add comment · Show 1 · 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 Jonster · Mar 09, 2013 at 03:23 AM 0
Share

I appreciate you taking the time to help Landon :)

avatar image
0
Wiki

Answer by Jonster · Mar 09, 2013 at 03:30 AM

I gave that a try but I am still getting the same error for some reason. Did i implement your method wrong?

stonyScript.js = GameObject.Find("Stony").GetComponent(stonyScript.js);

currentHeight = stonyScript.js.score

Comment
Add comment · Show 1 · 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 Jonster · Mar 09, 2013 at 02:20 AM 0
Share

"Stony" is the name of the gameObject associated with stonyScript

avatar image
0

Answer by RyanZimmerman87 · Mar 09, 2013 at 03:31 AM

The easiest way I believe is to just make your variables public static if you want to easily access them.

for example:

Script #1 is called PlayerMoveScript and contains:

public static int playerHealth = 10;

You can access this variable from any other script by using:

PlayerMoveScript.playerHealth += 1;

or:

int newPlayerHealth;

newPlayerHealth = PlayerMoveScript.playerHealth;

Comment
Add comment · Show 1 · 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 Jonster · Mar 09, 2013 at 03:38 AM 0
Share

You're right Ryan. In the link Jbarba posted I read about another question similar to $$anonymous$$e where the solution was to make the variable public static. $$anonymous$$y code is working correctly now, thanks to you guys :)

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

13 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

Related Questions

Accessing A Variable From Another Script 4 Answers

Accessing another var on another script 1 Answer

Accessing variable of script on an instance. 1 Answer

Is it possible to change a variable, into a script not assigned to any game object? 3 Answers

Problem with accessing variable form other scripts. 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