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 eeveelution8 · Jun 12, 2015 at 10:27 PM · errorvariableenummissingreferenceexception

Shop system using Enum throws up errors.

This is a simplified part of the script where it doesn't work,

 enum Shoptype {R_Rock,R_Iron,R_Silver,R_Gold,R_Gems,R_Uranium,R_Voidium,R_Zarkotium,R_Omnium,ShipFuel,ShipHull,ShipMissiles};
 var whichResource : Shoptype;
 var ThePlayer : PlayerScript;
 
 
 function OnMouseDown(){
     if(sellbutton == true){
         if(sellall == false){
             if(ThePlayer.whichResource >= amount){
                 ThePlayer.whichResource -= amount;
                 ThePlayer.Money += (price*amount);
                 guiTexture.color = Color(0.25,0.25,1,1);
             
             }else if(ThePlayer.whichResource < amount){
                 guiTexture.color = Color(1,0.25,0.25,1);
             
             }
         }if(sellall == true){
             if(ThePlayer.whichResource >= 1){
                 theamount = ThePlayer.whichResource;
                 ThePlayer.Money += (price*theamount);
                 ThePlayer.whichResource -= theamount;
                 guiTexture.color = Color(0.25,0.25,1,1);
             
             }else if(ThePlayer.whichResource == 0){
                 guiTexture.color = Color(1,0.25,0.25,1);
             
             }
         }
     }else if(sellbutton == false){
         if(ThePlayer.Money >= price){
             ThePlayer.whichResource += theamount;
             ThePlayer.Money -= price;
             guiTexture.color = Color(0.25,0.25,1,1);
         }else if(ThePlayer.Money < price){
             guiTexture.color = Color(1,0.25,0.25,1);
         }
     }
 }


it puts this error at the line,

 if(ThePlayer.whichResource >= amount){


"NullReferenceException: Object reference not set to an instance of an object"

not sure what's going on here, I don't work with enums alot.

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 Wideopen411 · Jun 12, 2015 at 10:46 PM 0
Share

I don't want to list this as an answer because I am not fluent enough in JS to know for sure, but is it looking for the playerscript in the inspector? So if you have the script assigned to something, look at it in the inspector. There is probably a slot for ThePlayer. Drag your player prefab into that slot. If there is no slot. Then make the top into a public var. so

 public var ThePlayer : PlayerScript;

Now it will be looking for your playerscript in the inspector. Drag your player prefab into that slot, and it will get the playerscript off of it. Hopefully that helps. I'm not really sure about JS but it seems its having trouble finding what the ThePlayer is.

Edit* This is assu$$anonymous$$g that the PlayerScript is attached to your player prefab. If it is, then it will know what to do after you drag the player prefab onto the ThePlayer slot in the inspector

avatar image eeveelution8 · Jun 12, 2015 at 11:03 PM 0
Share

I have the line

 function Start(){
     ThePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent(PlayerScript);
 }


In the script, so It finds the player and assigns the player prefab automatically, but its the sell button that doesn't work for some reason.

Strangely, the line, that makes the button a Buy Option works,

    }else if(sellbutton == false){


but sell doesn't.

These are the players stats if it helps solve the porblem,

 var $$anonymous$$oney : int = 0;
 
 var R_Rock : int = 0;
 var R_Iron : int = 0;
 var R_Silver : int = 0;
 var R_Gold : int = 0;
 var R_Gems : int = 0;
 var R_Uranium : int = 0;
 var R_Voidium : int = 0;
 var R_Zarkotium : int = 0;
 var R_Omnium : int = 0;

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

hashtable: snapshot out of sync 2 Answers

When i call some variables to an other second script i have a problem 1 Answer

Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable 1 Answer

Using an enum in a class constructor. 1 Answer

Error "There is already a local variable with the name 'obstacle'." How can I make it work? 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