HELP PLEASE !!! Calculating variables inside scripts with the same TAGS C# only

Ok I feel terrible having to ask people in the forum to figure this out for me. But it has been 5 days experimenting and I’m starting to go mad, yep cups of coffee thrown everywhere. So please help me with this, i’m only interested in doing this in C#. I have tried to use unity reference materials and wiki ect but their code seems to give me problems.

ALSO !!! if you have a paypal account I will send you some money, a little token of thank you for helping me. (the code that works well)

What I have is different regions eg Lisbon, Madrid, Paris ect. each object with their own name and script.

So attached to Lison object is a script called Lisbon example:

public class Lisbon : MonoBehaviour {

public static string Region = “Lisbon”;
public static string RegionOwner = “Portugal”;
public static int Population = 15000;
public static int PopulationGrowthRate = 2 ;
public static float ResidentialTaxRate = 20;
public static int RegionMoneyGrowth = 20;
public static int RegionID = 2;

void OnMouseDown()
{

mainInterface.RegionShow = Region;
mainInterface.RegionOwnerShow = RegionOwner;
mainInterface.PopulationShow= Population;
mainInterface.PopulationGrowthRateShow = PopulationGrowthRate;
mainInterface.ResidentialTaxRateShow = ResidentialTaxRate;
mainInterface.RegionMoneyGrowthShow = RegionMoneyGrowth;
mainInterface.RegionIDShow = RegionID;

}

}

madrid script is the same with different values ect

each object has a tag with “Portugal” which is the current owner. So madrid and lisbon has a tag called “Portugal”

I have a script called TurnCalc which I want to go through each object with TAG “Portugal” and get it’s Population value and then calculate them.

So in other words I when player clicks next turn the script called TurnCalc will go through each region and calculate the total population of each country. I hope that makes sence lol.

I have tried so many methods and I always get restricted by unity’s engine, there are so many I dont know where to begin. I think if I dont get this working soon I will have to purchase another engine. which would be a shame.

and please dont bother sending me links to unitys official manual and ref as I have hatred it. arrrhhh

Step 1
Change your main class to be generic for all the countries you’re dealing with so something like this:

public class Country : MonoBehaviour {
	public string Region = "Lisbon";
	public string RegionOwner = "Portugal";
	public int Population = 15000;
	public int PopulationGrowthRate = 2 ;
	public float ResidentialTaxRate = 20;
	public int RegionMoneyGrowth = 20;
	public int RegionID = 2;


	void OnMouseDown() {
		mainInterface.RegionShow = Region;
		mainInterface.RegionOwnerShow = RegionOwner;
		mainInterface.PopulationShow= Population;
		mainInterface.PopulationGrowthRateShow = PopulationGrowthRate;
		mainInterface.ResidentialTaxRateShow = ResidentialTaxRate;
		mainInterface.RegionMoneyGrowthShow = RegionMoneyGrowth;
		mainInterface.RegionIDShow = RegionID;
	}
}

This should also save you ending up with a ton of classes and any variables you need to change you can do that in the editor.

Step 2
Tag all countries with the same tag (i.e. “Country”).

Step 3
And finally, use a function like this to calculate your total value and do whatever you want with it.
In this function tagToSearch is the tag you chose in Step 2 and countryName is the name of the country you want the totals for:

public int CalculatePopulation(string tagToSearch, string countryName) {
	int populationCount = 0;
	
	GameObject[] countries = GameObject.FindGameObjectsWithTag(tagToSearch);
	for(int i = 0; i < countries.Length; i++) {
		Country currentCountry = countries[i].GetComponent<Country>();
		if(currentCountry.RegionOwner == countryName)
			populationCount += currentCountry.Population;
	}
}

P.S. I gladly take donations so if you’re happy with the solution then please let me know and I’ll PM my email address to you :smile:

I can guarantee you that the problems you’re running into here are not due to restrictions imposed by Unity. What you’re talking about here is just programming, and really isn’t directly related to (or affected by) Unity itself.

Anyway, just wanted to mention that, as it’d be a shame to switch engines over something like this :slight_smile:

Aerozo Thank you :slight_smile: I’m going to try this out now. From what I can see it looks right, well the logic… I tried a doing this using a foreach loop, but it failed.

“This should also save you ending up with a ton of classes and any variables you need to change you can do that in the editor.” Yep I did try it this way but then I had compile issues with instance and referencing which drove me mad… lol yep I’m new to this and need to read up alot about c# and unity.

Jesse Anders your right… I just get blinded with anger… I don’t understand why I cant use a string Variable to ref a script and this is making things anoying… for example I can communicate to a script variable : scriptname.varible but if I want to hold the name of scriptname as a string and communicate this way I cant… unity won’t allow it… but why oh why??? this bugs me…

Aerozo how long have you been working with unity? Also where in the UK are you… London? after I have tested this script, if it works I will send pm you and arrange to send you payment… also I’m waiting for another customer to buy from me using paypal so I can refund a customer as my paypal funds is empty due to a big payment sent out to my supplier… hmm bummer… But I’m a man of my word and a litle payment will be sent :slight_smile: … although I’m not a rich man, but I think I’m going to need you in the future and will be happy to pay.

Ok now Test time. Thanks Again Aerozo.

Aerozo I’m getting this error on compile:

error CS0501: `TurnCalc.CalculatePopulation(string, string)’ must have a body because it is not marked abstract, extern, or partial

lol I wish the compile errors would explain better lol I’ve removed all mugs of coffee from my desk :slight_smile:
hmm what does body mean??

anyway Aerozo Thanks again

Hey Aerozo it works !!! yeeeeeeeeeeeeeeeeeeeeyyyyyyyyyyyyyy !!!

I just took out public int CalculatePopulation(string tagToSearch, string countryName)

private string tagToSearch = “Portugal”;
private string countryName = “Portugal”;

and it works a treat… you are a genius… I google the error and it came up with something todo with net version bla bla which I didn’t know they was talking about … lol yep I’m super noob at this. anyway it works better than I expected and I can doing this way can mean I can cut out a lot of if = this then do this and that ect. So Thank you again and payment will be sent within a day… can you please pm me your paypal email address :slight_smile:

And we need to talk about a few things so when you have time maybe we could talk on skype.

Hehe, sure, it’s easy to get frustrated sometimes :slight_smile:

The ‘string’ thing is more of a language issue than a Unity issue. That said, you actually can reference a variable using a string, by using reflection. However, it’s kind of a low-level approach, and isn’t the optimal solution in most cases.

What solutions are appropriate depends on the circumstances, but one option is to use a dictionary to associate values with strings (in this case all of the values have to be of the same type, but it does allow you to access the values by name).

Sorry, I forgot to mention that function should go inside a Monobehaviour and you just call it whenever you wanna calculate the population of any country that’s been set up with the country class.

Yep! Well, pretty close anyway. I’ve PMed you my Skype, speak to you on there :slight_smile: