Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by brendenderp · Jul 21, 2017 at 04:16 AM · scripting problemgetcomponentvariablesonmousedownupgrades

{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons.

so basically im trying to start off my upgrades system in my game. my first thought was to add an upgrade that can be bought so you can heal your self. so i made some code as seen below.

the problem is that it works if its blank but not once I put in the code to get .money and the .coin values (.coin is an audio source, while .money is an int) here is the code that works

     void OnMouseDown() {
 
         Debug.Log ("clicked");
     
         //prints out that the item was clicked successfully! yay 
     
     }


but here is the code that doesnt work for some reason.

     void OnMouseDown() {
 
         Debug.Log ("clicked");
     // noting is printed and noting happens. (the void() update loop does run though)
         
         mon = ship.GetComponent<mover>().money;
     coi = ship.GetComponent<mover> ().coin;
     if (mon > 10) {
             ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
             ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
             coi.Play ();
         }
     }





After alot testing it seems that setting the mon value and the coi value are the problem somehow. but it makes no sense. because even if they are messing it up then for 1, why is there no compiler errors. and for 2, why isnt the debug log being printed?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brendenderp · Jul 21, 2017 at 04:29 AM

further more trying to solve the issure i thought mabye its because im adding the health inside of onmouseDown? nope that wasnt it. so i tryed moving the code to void update... nope still noting.

here is what im at so far. (still non functional ) using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class buyHP : MonoBehaviour {
 
     public int mon;
     public GameObject ship;
     public AudioSource coi;
     public bool clk =false;
 
     void OnMouseUp() {
         // this is completely ignored by the compiler and unity. anything put in here is not ran.
         Debug.Log ("clicked");
     
         clk = true;
     
     }
         
 
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         //it prints this!
         Debug.Log ("Running");
 
         //this only gets ran if i change clk manualy in the inspector. 
         if (clk == true) {
             mon = ship.GetComponent<mover> ().money;
             coi = ship.GetComponent<mover> ().coin;
             if (mon > 10) {
                 ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
                 ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
                 coi.Play ();
             }
             clk = false;
         }
     }
 }
 
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 brendenderp · Jul 21, 2017 at 04:53 AM 0
Share

so now, since it seems as though there is no reason for this to be happening, i ended up fixing it. not by making my code better. but buy making it worse basically. i had to remove all the variables and ins$$anonymous$$d purely reference their counterpart in the mover script. so no it works. but its also ugly. here you go in case anyone needs this. (if any one from the unity higher ups is reading this PLEASE fix this issue there is no reason for the above code not to work compared to this one) ... NEVER $$anonymous$$IND .... this code actualy doesnt work. it worked once. i recompiled and now it doesn't???

 public class buyHP : $$anonymous$$onoBehaviour {
 
     public int mon;
     public GameObject ship;
 
     public bool clk =false;
 
     void On$$anonymous$$ouseDown() {
         // this is completely ignored by the compiler and unity. anything put in here is not ran.
 
         clk = true;
 
     
     }
         
 
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         //it prints this!
         Debug.Log ("Running");
 
         //this only gets ran if i change clk manualy in the inspector. 
         if (clk == true) {
 
 
 
         
             if (ship.GetComponent<mover> ().money > 10) {
                 ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
                 ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
              ship.GetComponent<mover> ().coin.Play();
 
 
             }
             clk = false;
         }
     }
 }

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

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

Related Questions

For with float i returning wrong number. 0 Answers

NullReferenceException on getComponent 1 Answer

Randomized array result from other scipt 2 Answers

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

C# Unity dot syntax exercise correct solution? 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