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 /
avatar image
0
Question by kaji0tt · Mar 08, 2019 at 03:09 AM · buttonscript.if-statementsonclickevent triggering

Instantiate on click if enough gold / ressources

Hi there, I have made an UI with some buttons and I'd like to instantiate things when i click those, but only if i have enough gold. Spawning works fine without the if statement

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnCreep : MonoBehaviour
 {
     public GameObject Creep;
     public Transform Spawnpoint;
     public int Cost;
     private crntGold gold;
 
    
 
     
    
     public void SpawnThis()
     {
         
         //gold = GetComponent<crntGold>();
         
 
           //  if (gold.gold >= 10)
           //  {
 
                    Instantiate(Creep, Spawnpoint.position, Spawnpoint.rotation);
            //     gold.gold = gold.gold - Cost;
 
            //    }
 
 
     }
 
 
 }

how come, and how could i change this? for sake of completeness, here is my gold script, maybe the error is there:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class crntGold : MonoBehaviour
 {
     public int gold;
     public Text uigold;
     private GoldCounter goldCounter;
 
     void Start()
     {
         gold = 100;
         uigold.text = "Gold:" + gold.ToString();
         goldCounter = GetComponent<GoldCounter>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (goldCounter.timer == 0.0f) ;
         { 
             
             uigold.text = "Gold:" + gold.ToString();
            
         }
         
     }
 }
 


thanks for any replys in advance!

Comment
Add comment · Show 1
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 Tsaras · Mar 08, 2019 at 06:02 AM 0
Share

Are you sure the component crntGold component is attached to the same object that the SpawnCreep script is attached to?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mr_parkour10021 · Mar 08, 2019 at 04:14 AM

Hi there, @kaji0tt try to Debug.Log(); your gold.gold to check the actual value.

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 kaji0tt · Mar 08, 2019 at 04:50 AM 0
Share

It says "NullReferenceException: Object reference not set to an instance of an object" as gold.gold appears.. however, im succesfully fetching this instance with my income counter script. It adds 10 gold to the crntGold evéry 10 seconds, with the same procedure of geting the component

Just checked Income Counter with Debug - works great, i have 100 "gold.gold" at start, +10 every 10 seconds.

avatar image
0

Answer by kaji0tt · Mar 08, 2019 at 04:01 PM

The Tab get component was opened for the last 15 hours, however it took me this time to realise, that i had to fetch the gold parameter with "gold = Manager.GetComponent();" with my script manager added to the button.

Thank you @Mr_parkour10021 for introducing me to debugging - probably this was the more efficent way of learning.

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

131 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

Related Questions

How do i make the perfect button? 1 Answer

How do I attach a GameObject when the script is attached to unity's on click () function (for buttons). 2 Answers

void dont show up in on click 1 Answer

OnClick() animation 0 Answers

[Solved] Button OnClick properties are missing after loading the scene 4 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