Upgrade multiplier for clicker game such as Adventure Capitalist - c#

Hello community I am making a clicker game but I don't know how to do the upgrade multiplier button such as Adventure Capitalist, I have a single script attached to each GameObject, total of six objects with the same script attached, where I have the Text object which shows a double value which is an array with six indexes, the problem here is that I have the multiplier button object attached with another script, I should do something as static (singleton) because each script has many objects, I only need to access to a single text which is upgrade price displayer crop which is non-static. Pls help me.
This is the code for the multiplier button
public class MultiplyManager : MonoBehaviour {
public void multiply_Crop_PerOne()
{
UpgradeButtonCrop.multiplier = 1;
}
public void multiply_Crop_PerTen()
{
UpgradeButtonCrop.multiplier = 10;
}
public void multiply_Crop_PerHundred()
{
UpgradeButtonCrop.multiplier = 50;
}}
And this is the code for the text UI which I am working, what I want to do is make the multiplier button multiply the cost of the upgrade and increase its level, for example if I have an upgrade which costs 1 gold, the multiplier per ten should make that upgrade cost for 10 and increase 10 levels.
public class UpgradeButtonCrop : MonoBehaviour{
public static int multiplier;
public Button upgradeButton_crop;
public TMP_Text level_displayer_crop;
public TMP_Text perSec_displayer_crop;
public TMP_Text upgrade_price_displayer_crop;
[HideInInspector]
public double startGoldByUpgrade_crop;
public double startCurrentCost_crop;
public double[] crop_upgrade_price_arr = new double[6];
public int[] level_crop_arr = new int[6];
public void First_Crop_Button()
{
purchaseUpgradeCrop(0);
}
public void Second_Crop_Button()
{
purchaseUpgradeCrop(1);
}
public void Third_Crop_Button()
{
purchaseUpgradeCrop(2);
}
public void Fourth_Crop_Button()
{
purchaseUpgradeCrop(3);
}
public void Fifth_Crop_Button()
{
purchaseUpgradeCrop(4);
}
public void Last_Crop_Button()
{
purchaseUpgradeCrop(5);
}
public void purchaseUpgradeCrop(int index)
{
if ((level_crop_arr[index] + multiplier) <= max_level)
{
if (DataController.Instance.gold >= crop_upgrade_price_arr[index] * multiplier)
{
updateUI_Crop(index);
updateUpgrade_Crop();
upgrade_price_displayer_crop.text = LargeNumber.ToString(crop_upgrade_price_arr[index]).ToString();
DataController.Instance.saveUpgradeButton_Crop(this);
}
}
else
{
UpgradeMessageManager.instance.Start_coroutine_cant_upgrade();
return;
}
}
}

You can find components in the scene;
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
So you could do something like this;
class UpgradeButtonCrop : Monobehaviour {
MultiplyManager _manager;
void Start()
{
// Find the manager and save a reference
_manager = FindObjectsOfType<MultiplyManager>().First();
}
}
and since you now have a reference to that manager you can do something with it.
Also see this tutorial;
https://www.youtube.com/watch?v=-kd68uKt4jk

Related

UNity3d Adding a value via button and getting random results

i found my next tier 1 problem and hoping for some help here in the so called internet.
first of all here are my atm not to hard to understanding codes.
please tell me if im doing something wrong here on stackoverflow im an absolute newbie and dont know how to explain my problem in this case without post a bunch of code :(
i know there are much ways to improve mystuff but im in the learning process and for now its maybe enough to tell my why the behavior of my code didnt match my expectations.
This is my PlayerClass:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Values : MonoBehaviour
{
public float MaxNrg;
public float Nrg;
public float NrgDrain;
public float NrgDrainCoef;
public float NrgInPercent;
// Start is called before the first frame update
public void Start()
{
MaxNrg = 7950.0f;
Nrg = 7950.0f;
NrgDrain = 0.09201389f;
NrgDrainCoef = 1.0f;
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
NrgInPercent = (GETnrg() / GETmaxnrg()) * 100;
}
public void SETmaxnrg(float tmpfloat)
{
MaxNrg = tmpfloat;
}
public void SETnrg(float tmpfloat)
{
Nrg = tmpfloat;
}
public void SETnrgdrain(float tmpfloat)
{
NrgDrain = tmpfloat;
}
public void SETnrgdraincoef(float tmpfloat)
{
NrgDrainCoef = tmpfloat;
}
public void ADDmaxnrg(float tmpfloat)
{
MaxNrg += tmpfloat;
}
public void ADDnrg(float tmpfloat)
{
Nrg += tmpfloat;
}
public void ADDnrgdrain(float tmpfloat)
{
NrgDrain += tmpfloat;
}
public void ADDnrgdraincoef(float tmpfloat)
{
NrgDrainCoef += tmpfloat;
}
public float GETmaxnrg()
{
return MaxNrg;
}
public float GETnrg()
{
return Nrg;
}
public float GETnrgdrain()
{
return NrgDrain;
}
public float GETnrgdraincoef()
{
return NrgDrainCoef;
}
public float GETnrginpercent()
{
return NrgInPercent;
}
}
End Here are the "Drain_mechanics":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drain_Mechanics : MonoBehaviour
{
public bool drainstate = false;
public double NrgLeftInSeconds;
public GameObject Scriptcontainer_Player;
// Start is called before the first frame update
public void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
if(drainstate == true)
{
Scriptcontainer_Player.GetComponent<Player_Values>().Nrg -= Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain() * Time.deltaTime * Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef();
}
else if(drainstate == false)
{
}
NrgLeftInSeconds = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrg() / (Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain() * Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef());
}
public void SETdrainstate(bool tmpdrainstate)
{
drainstate = tmpdrainstate;
}
public double GETnrgleftinseconds()
{
return NrgLeftInSeconds;
}
}
Class for initializing the input fields in the game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Initialize_InputFields : MonoBehaviour
{
public GameObject Input_Energy;
public GameObject Input_Drainrate;
public GameObject Input_DrainCoef;
public float InputNrg;
public float InputNrgDrainrate;
public float InputNrgDrainCoef;
// Start is called before the first frame update
public void Start()
{
Input_Energy = GameObject.Find("Input_Energy");
Input_Drainrate = GameObject.Find("Input_Drainrate");
Input_DrainCoef = GameObject.Find("Input_DrainCoef");
}
// Update is called once per frame
void FixedUpdate()
{
InputNrg = float.Parse(Input_Energy.GetComponent<InputField>().text);
InputNrgDrainrate = float.Parse(Input_Drainrate.GetComponent<InputField>().text);
InputNrgDrainCoef = float.Parse(Input_DrainCoef.GetComponent<InputField>().text);
}
public float GETinputnrg()
{
return InputNrg;
}
public float GETinputnrgdrainrate()
{
return InputNrgDrainrate;
}
public float GETinputnrgdraincoef()
{
return InputNrgDrainCoef;
}
}
a class to manipulate the textfields while the game is running
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Display_LabelsCalculated : MonoBehaviour
{
public GameObject Scriptcontainer_Player;
public GameObject Scriptcontainer_GameMechanics;
public GameObject Display_Energy;
public GameObject Display_EnergyDrain;
public GameObject Display_EnergyDrainCoef;
public GameObject Display_EnergyTimeleft;
public GameObject Display_EnergyPercentleft;
public Text Display_Energy_Text;
public Text Display_EnergyDrain_Text;
public Text Display_EnergyDrainCoef_Text;
public Text Display_EnergyTimeleft_Text;
public Text Display_EnergyPercentleft_Text;
// Start is called before the first frame update
void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
Scriptcontainer_GameMechanics = GameObject.Find("Scriptcontainer_GameMechanics");
Display_Energy = GameObject.Find("Display_Energy");
Display_EnergyDrain = GameObject.Find("Display_EnergyDrain");
Display_EnergyDrainCoef = GameObject.Find("Display_EnergyDrainCoef");
Display_EnergyTimeleft = GameObject.Find("Display_EnergyTimeleft");
Display_EnergyPercentleft = GameObject.Find("Display_EnergyPercentleft");
Display_Energy_Text = Display_Energy.GetComponent<Text>();
Display_EnergyDrain_Text = Display_EnergyDrain.GetComponent<Text>();
Display_EnergyDrainCoef_Text = Display_EnergyDrainCoef.GetComponent<Text>();
Display_EnergyTimeleft_Text = Display_EnergyTimeleft.GetComponent<Text>();
Display_EnergyPercentleft_Text = Display_EnergyPercentleft.GetComponent<Text>();
}
// Update is called once per frame
public void Update()
{
}
public void FixedUpdate()
{
int hours = (int)(Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() / 60) / 60;
int minutes = (int)(Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() / 60) % 60;
int seconds = (int)Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().GETnrgleftinseconds() % 60;
Display_EnergyTimeleft_Text.text = hours.ToString() + ":" + ((minutes < 10) ? ("0") : ("")) + minutes.ToString() + ":" + ((seconds < 10) ? ("0") : ("")) + seconds.ToString();
Display_Energy_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrg().ToString();
Display_EnergyDrain_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdrain().ToString();
Display_EnergyDrainCoef_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrgdraincoef().ToString();
Display_EnergyPercentleft_Text.text = Scriptcontainer_Player.GetComponent<Player_Values>().GETnrginpercent().ToString();
}
}
followed by some button mechanics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Button_Mechanics : MonoBehaviour
{
public GameObject Scriptcontainer_Player;
public GameObject Scriptcontainer_InputFields;
public GameObject Scriptcontainer_GameMechanics;
public Button Button_SetEnergy;
public Button Button_SetDrainCoef;
public Button Button_SetDrainrate;
public Button Button_AddEnergy;
public Button Button_AddDrainCoef;
public Button Button_AddDrainrate;
public Button Button_ActivateMechanics;
public Button Button_DeactivateMechanics;
public Button Button_ResetValues;
// Start is called before the first frame update
public void Start()
{
Scriptcontainer_Player = GameObject.Find("Scriptcontainer_Player");
Scriptcontainer_InputFields = GameObject.Find("Scriptcontainer_InputFields");
Scriptcontainer_GameMechanics = GameObject.Find("Scriptcontainer_GameMechanics");
Button_SetEnergy = GameObject.Find("Button_SetEnergy").GetComponent<Button>();
Button_SetDrainCoef = GameObject.Find("Button_SetDrainCoef").GetComponent<Button>();
Button_SetDrainrate = GameObject.Find("Button_SetDrainrate").GetComponent<Button>();
Button_AddEnergy = GameObject.Find("Button_AddEnergy").GetComponent<Button>();
Button_AddDrainCoef = GameObject.Find("Button_AddDrainCoef").GetComponent<Button>();
Button_AddDrainrate = GameObject.Find("Button_AddDrainrate").GetComponent<Button>();
Button_ActivateMechanics = GameObject.Find("Button_ActivateMechanics").GetComponent<Button>();
Button_DeactivateMechanics = GameObject.Find("Button_DeactivateMechanics").GetComponent<Button>();
Button_ResetValues = GameObject.Find("Button_ResetValues").GetComponent<Button>();
}
public void Update()
{
}
// Update is called once per frame
public void FixedUpdate()
{
Button_SetEnergy.onClick.AddListener(SETnrgbtnclk);
Button_SetDrainCoef.onClick.AddListener(SETnrgdraincoefbtnclk);
Button_SetDrainrate.onClick.AddListener(SETnrgdrainbtnclk);
Button_AddEnergy.onClick.AddListener(ADDnrgbtnclk);
Button_AddDrainCoef.onClick.AddListener(ADDnrgdraincoefbtnclk);
Button_AddDrainrate.onClick.AddListener(ADDnrgdrainbtnclk);
Button_ActivateMechanics.onClick.AddListener(SETactivatemechanicsbtnclk);
Button_DeactivateMechanics.onClick.AddListener(SETdeactivatemechanicsbtnclk);
Button_ResetValues.onClick.AddListener(SETresetbtnclk);
}
public void SETnrgbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrg(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrg());
}
public void SETnrgdrainbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdrain(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdrainrate());
}
public void SETnrgdraincoefbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdraincoef(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdraincoef());
}
public void ADDnrgbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrg(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrg());
}
public void ADDnrgdrainbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrgdrain(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdrainrate());
}
public void ADDnrgdraincoefbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().ADDnrgdraincoef(Scriptcontainer_InputFields.GetComponent<Initialize_InputFields>().GETinputnrgdraincoef());
}
public void SETactivatemechanicsbtnclk()
{
Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().SETdrainstate(true);
}
public void SETdeactivatemechanicsbtnclk()
{
Scriptcontainer_GameMechanics.GetComponent<Drain_Mechanics>().SETdrainstate(false);
}
public void SETresetbtnclk()
{
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrg(7950);
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdrain(0.09201389f);
Scriptcontainer_Player.GetComponent<Player_Values>().SETnrgdraincoef(1.0f);
}
}
at this point i apollogize one more time the amount of text im posting here but otherwise i cant even explain whats going on. and im not able to pick the exact codesnippets which are needed here. it will be better in future.
basically the main mechnisms work fine and im absolutely proud of what i did here...
my problems are the following.
Major problem 1. when i enter a value in the input fields and perform the button SET actions, i HAVE to do it in a specific order. NRG->DRAIN->COEF
otherwise the calculated textboxes are doing random stuff.
Major problem 2. when i try to perform the button add actions, the game also do random stuff. even when i set all variable to fixed values.
minor problem 3. in the Initialize_InputFields.cs im using the float.parse functionality. and my game constantly create warnings about a formatexception. is there a smarter way to solve this.
minor problem 4. last but not leat i want to work with realtime values. is there a way in unity to accerlate the globale timescale for the whole game?

Shared GameSettings with U-Net

I have a scene where the player has the option to choose settings for the match they are creating (number of rounds, time per round etc..), I also have a utility class MatchSettings that contains all of these settings, when I run the game on the host everything works fine, however when a client joins the game, the clients match settings are 0 for everything, The settings are used as part of a GameManager class that implements a singleton pattern with a MatchSettings member. So my question is how can I have all the participants of the game share the same settings? ( I am aware that u-net is deprecated)
The Relevant Code for the GameManager:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public MatchSettings settings;
void Awake()
{
if(instance != null)
{
Debug.LogError("Too many game managers");
}
else
{
instance = this;
respawnCamera.SetActive(false);
settings = new MatchSettings();
timePassed = settings.roundTime * 60;
roundsPlayed = 0;
highestKills = 0;
}
}
void Update()
{
timePassed -= Time.deltaTime;
if (timePassed < 0 || highestKills >= settings.maxKills)
{
Debug.Log(settings.roundTime); //prints 0 at client runtime
RoundOver();
}
if(roundsPlayed >= settings.roundCount)
{
GameOver();
}
}
}
The relevant code for the MatchSettings:
[System.Serializable]
public class MatchSettings
{
public float roundovertime = 10f;
public static float roundtime; // the variables from the UI scene are stored in the static members and transferred
public static int maxkills; // into the regular ones when MatchSettings() is called [in awake in game manager]
public static int roundcount;
public float respawntime = 5f;
public float roundTime;
public int maxKills;
public int roundCount;
public MatchSettings()
{
roundTime = roundtime;
maxKills = maxkills;
roundCount = roundcount;
}
}
Thanks in advance!
Unless you synchronize MatchSettings to all clients, you will always have the default values there (zeros in this case).
One way about it using UNET is using SyncVar - You will need to have the settings on a gameobject in the scene, owned by the server which will be your "source of truth".
You only perform changes on the server side, and it will be automatically updated to all clients.
Pseudo-code example:
class GameSettings : NetworkBehaviour {
[SyncVar(hook=nameof(FragsRequiredAmountSyncVarChanged))] private int _fragsRequiredToWinSyncVar = 20;
public void ChangeFragsRequiredToWin(int newAmount) {
if (!isServer) {
Debug.LogError("Sync vars can only change on the server!");
return;
}
_fragsRequiredToWinSyncVar = newAmount;
}
private void FragsRequiredAmountSyncVarChanged(int newAmount)
{
Debug.Log($"Frag requirement changed to {newAmount}");
}
}
I've also included an example on how to attach hooks when the SyncVar changes; I'm pretty sure it gets called on both the server and the client, but my memory might fail me since it's been quite a while since I last used UNET.

Working with Scriptable Objects: Keep getting "No suitable method found to override"?

Currently teaching myself on proper use of Scriptable Objects and how to get them to communicate with other scripts. Getting the error "No suitable method found to override". Done my own research and it is always because of a typo or because something was not exactly the same. I dont see either of those problems in my script. It is so basic I have looked over it like 20 times.
public class DamageFormula : MonoBehaviour
{
public int Attack;
public int Power;
public int Damage;
// Start is called before the first frame update
public void Start()
{
Power = 0;
}
// Update is called once per frame
void Update()
{
CalculateDamage(Attack, Power);
}
void CalculateDamage(int Attack, int Power)
{
Damage = Attack * Power;
}
}
Here is base class for skill:
public abstract class BaseSkills : ScriptableObject
{
public int Power;
public abstract void Start(GameObject BattleManager);
}
Here is the first skill: Problem is at public override void Start (Gameobject BattleManager)
[CreateAssetMenu(menuName = "Skills/HeadButt")]
public class HeadButt : ScriptableObject
{
public int Power = 30;
private DamageFormula damageFormula;
public override void Start(GameObject BattleManager)
{
damageFormula = BattleManager.GetComponent<DamageFormula>();
damageFormula.Start();
damageFormula.Power = Power;
}
}
What is the problem? What does it want me to do?

Better way to count all existing citizens?

I've started making a simple city builder game, in the spirit of Zeus/Poseidon, but much simpler. I have the grid system ready and ability to add houses and roads. Yesterday I began to add citizens, in a simple way, that is, whenever a house is created, 5 people are created and move directly from one edge of the map to that particular house. Once they reach that particular house, I consider they became citizens, and add them to the list of residents of the house, and also to the list of citizens of the city.
For that, each house instance has a List of Human, and my Game class which contains all the information of the game also has one List of human.
To simplify it looks like this:
Game.cs
public class Game {
private static Game instance; // this is a singleton
private int currentAmount; //this is the value I'm using to display the number of citizens on screen
private List<Human> humen;
public List<Human> Humen
{
get { return humen; }
set
{
humen = value;
currentAmount = humen != null ? humen.Count : 0;
}
}
public void AddHuman(Human human)
{
humen.Add(human);
currentAmount = humen.Count;
}
/// <summary>
/// Private constructor to ensure it's only called when we want it
/// </summary>
private Game()
{
humen = new List<Human>();
}
public static void setGame(Game game)
{
instance = game;
}
/// <summary>
/// Returns the instance, creates it first if it does not exist
/// </summary>
/// <returns></returns>
public static Game getInstance() {
if (instance == null)
instance = new Game();
return instance;
}
}
House.cs
public class House : Building {
public static int CAPACITY = 5;
private List<Human> habitants;
public List<Human> Habitants
{
get { return habitants; }
set { habitants = value; }
}
public House() {
habitants = new List<Human>();
}
}
HumanEntity.cs
public class HumanEntity : MonoBehaviour {
private Human human;
private float speed;
public Human Human
{
get { return human; }
set { human = value; }
}
// Use this for initialization
void Start () {
speed = Random.Range(5.0f, 10.0f);
}
// Update is called once per frame
void Update () {
if (human != null)
{
Vector3 targetPosition = human.Target.GameObject.transform.position;
if (transform.position.Equals(targetPosition)) {
if (!human.HasAHouse)
{
human.HasAHouse = true;
Game.getInstance().AddHuman(human); // here I'm adding the human to the list of citizens
((House)human.Target).Habitants.Add(human); // here I'm adding it to the house list of habitants
}
}
else {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
}
}
}
}
And this is working as expected, but I'm wondering if having one list of human by house in addition with a global list in the game class is not a little overkill, and if there was maybe a more elegant way to achieve that count on the Game class, maybe something more "Unity friendly" if I may say so, as I don't really know a lot about the capacities of Unity. Do you have any advice on what to do, is that okay to keep it this way or is there a more elegant way?
Fast and appropriate way to know how many human would be to have a static counter on HumanEntity class:
public class HumanEntity : MonoBehaviour
{
public static int HousedHuman { get; private set; }
public static int HumanCount { get; private set; }
void Awake() { HumanCount++; }
void OnDestroy()
{
HumanCount--;
if(human.HasAHouse == true){ HousedHuman--; }
}
public static void ResetCounter() { HouseHuman = HumanCount = 0; }
void Update () {
if (human != null)
{
Vector3 targetPosition = human.Target.GameObject.transform.position;
if (transform.position.Equals(targetPosition)) {
if (!human.HasAHouse)
{
HouseHuman++; // Added
human.HasAHouse = true;
// Rest of code
}
}
// Rest of code
}
}
}
When a new instance is added, the counter is increased, when the instance is destroyed, the counter is decreased.
You can access via HumanEntity.HumanCount. You won't be able to set it elsewhere than in the HumanEntity class.
Make sure to reset the counter when you start/leave the scene.
EDIT: based on comment, I added a second static counter for HousedHuman. This is increased when the entity reaches the house. It gets decreased when the entity is destroyed if the entity was housed. It also gets reset when needed along with the overall counter.
Building on Everts's idea...
Game:
public class Game {
private static Game instance; // this is a singleton
public static int currentAmount { get; set; }
//rest of class
}
House:
public class House : Building {
public static int CAPACITY = 5;
private List<Human> habitants;
public List<Human> Habitants
{
get { return habitants; }
set { habitants = value; }
}
public House() {
habitants = new List<Human>();
}
public void AddHuman(Human human)
{
human.HasAHouse = true;
habitants.Add(human);
Game.currentAmount++;
}
}
UpdateLoop:
// Update is called once per frame
void Update () {
if (human != null)
{
Vector3 targetPosition = human.Target.GameObject.transform.position;
if (transform.position.Equals(targetPosition)) {
if (!human.HasAHouse)
((House)human.Target).AddHuman(human);
}
else {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
}
}
}
If checking house capacity is required, you can change the AddHuman method to a bool return type, do a capacity check inside and return whether or not it was successfully added.
You can also add a RemoveHuman method that would count humans down via Game.currentAmount--;
As for the list in Game, it really depends on the context. The List in your Game class could be useful to differentiate between wandering humans, and humans who are housed, if this behavior is required. (Wandering humans in the Game list, housed in the housed list)

An Unity3D button appears to be null while trying to disable it

What I'm trying to do is disable the button when the time is up. But I get an error while I set the button to be disable: it appears to be null.
The following script (RightButton.cs) includes a function called DisableButton that is invoked by the main script (TappingEngine.cs).
The statement that hangs inside the DisableButton function is btn.interactable = false;.
/* RightButton.cs */
using UnityEngine;
using UnityEngine.UI;
public class RightButton : MonoBehaviour {
Button btn;
// Use this for initialization
void Start () {
Debug.Log("Right button ready.");
}
public void Tap()
{
Debug.Log("Tap right! tot: " + TappingEngine.te.nTaps);
TappingEngine.te.Tap(TappingEngine.tapType_t.right);
}
public void DisableButton()
{
btn = this.GetComponent<Button>();
btn.interactable = false;
}
}
The following code is a part of the main script (which is quite long). However, someone advised me to post the full version for clarity.
(Focus on the two functions Update () and DisableButtons().)
/* TappingEngine.cs */
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TappingEngine : MonoBehaviour {
public static TappingEngine te;
public int nTaps = 0;
public int GlobalTotal;
public int RightTaps;
public int LeftTaps;
public string FirstTap;
public int LastTap;
public int MaxChain;
public int FastestTwoTaps;
public int FastestTwoChainedTaps;
public GlobalTotalTaps GlobalTotalTaps_script;
public TotalRightTaps RightTotalTaps_script;
public TotalLeftTaps LeftTotalTaps_script;
public FirstTap FirstTap_script;
public RightButton RightButton_script;
const float TimeSpan = 5F;
public float Chrono;
public bool ChronoStart = false;
public bool ChronoFinished = false;
public float ElapsedPercent = 0F;
public enum tapType_t { right, left };
// Use this for initialization
void Start () {
GlobalTotal = 0;
te = this;
GlobalTotalTaps_script = FindObjectOfType(typeof(GlobalTotalTaps)) as GlobalTotalTaps;
RightTotalTaps_script = FindObjectOfType(typeof(TotalRightTaps)) as TotalRightTaps;
LeftTotalTaps_script = FindObjectOfType(typeof(TotalLeftTaps)) as TotalLeftTaps;
FirstTap_script = FindObjectOfType(typeof(FirstTap)) as FirstTap;
RightButton_script = FindObjectOfType(typeof(RightButton)) as RightButton;
}
// Update is called once per frame
void Update () {
if(ChronoStart) {
ElapsedPercent = (Time.time - Chrono) * 100 / TimeSpan;
if(ElapsedPercent >= 100F) {
DisableButtons();
//SceneManager.LoadScene("Conclusion", LoadSceneMode.Single);
}
}
Debug.Log("Elapsed time: " + (Time.time - Chrono));
}
private void DisableButtons()
{
RightButton_script.DisableButton();
}
public void OnTap() {
Debug.Log("Tap!");
}
public void Tap(tapType_t tapType) {
Manage_GlobalTotalTaps();
if(GlobalTotal == 1) {
Manage_FirstTap(tapType);
ChronoStart = true;
Chrono = Time.time;
}
Manage_LeftRight(tapType);
}
private void Manage_FirstTap(tapType_t tapType)
{
FirstTap = tapType.ToString();
FirstTap_script.FastUpdate();
}
private void Manage_LeftRight(tapType_t tapType)
{
if (tapType.Equals(tapType_t.left))
{
LeftTaps++;
LeftTotalTaps_script.FastUpdate();
}
else
{
RightTaps++;
RightTotalTaps_script.FastUpdate();
}
}
public void Manage_GlobalTotalTaps() {
GlobalTotal++;
GlobalTotalTaps_script.FastUpdate();
}
}
Below are a couple of screenshots that I hope will be useful.
Until now I have not yet figured out where the problem is. Moreover, the script of the button is very similar to that of the various Text objects that represent the statistics (the list on the right side of the screen).
With Text objects it works, with the Button object not.
I asked the same question on Unity3D Forum and I got the solution here.
One the game object where I attached the RightButton.cs there is a variable on line 24 Button btn; that needs to be set to public, so public Button btn;.
This makes appearing a new entry in the Inspector of the Right Button object (see the image below) and so in Unity3D drag and drop the Right Button GameObject into Right Button (Script)'s proper field (Btn in this case).

Categories