Ok so I am trying to check to see if the current string in a string[ ] matches a name.
Heres my code
for(int cnt = 0; cnt < Animals.Length; cnt++)
{
if(Animals[cnt].name.CompareTo(Prey[cnt].name) //How do I compare the two. The current animal string and the current name of the prey.
{
PreyInArea.Add(Animals[cnt].gameObject); // here I want to add to the PreyInArea the current animal gameobject
}
}
CompareTo just calls the default comparer for the type so you don’t need to call it directly, right? That is why you can just use the equality operator ‘==’. I’ve done a lot of custom comparers lately for a new tool we are releasing soon. It is pretty handy if you need a custom way to compare two things, but for built-in types, it is already implemented.
In order of fastest to slowest for comparing different things in the game world:
layers
id variables and fetching component (ie a script attached to the object with a var called id giving the number or identity)
tags
string comparison
Layers are the king for speed and large lists of comparisons because you’re just doing an integer check, while the second one is a component fetch before an integer check, which can be quicker if the string comparison is slower.
Tags and string comparison should be roughly the same speed.
Does anyone know if you can set a layer higher than 31? for internal purposes, ie setting an object’s layer to 200 - even if unity itself doesn’t use it?
Thanks for the help.
What I am comparing is a string in a array with another string in a array going back and forth.
So how would I do that with layers.
Can you explain in broader terms what the association is you are trying to make between these separate “things” (animals and prey)? I smell a dictionary coming.
animals holds all the “animals” in the scene.
prey are what a selected animal eat/chase after. Each animal has a list of its own prey.
Predators are what a selected animal fear/run away from. Each animal has its own list of predators
Sorry I should of explained.
Thanks
It is hard to offer advice on this amount of information and involvement, so take this for what ever it is worth… It sounds like you want an “Animal” class which has a member: List<Animal> prey = new List<Animal>();Then drag and drop all the prey in to the list for each animal in the prefab.
Also make a public variable “prefabGO” to hold a cache of the PREFAB’s GameObject for quick comparison. Right after you instantiate a new animal, give it a reference to the prefab that created it. (Note that you cannot have this as a drag and drop reference because Unity will change the reference to the clone, since it is not a prefab reference, it is a self-reference. )
Now you have a component with a list of prey Animal components with a prefabGO reference
For an example, say you have a list of Animals “in the area” (you never explained what this means, by the way) in a list called “animalsInArea”, and you want to compare everything in this list with “this” Animal (The one this script is on), and add it to another list this.preyInArea…
this.preyInArea.Clear();
foreach (Animal animal in animalsInArea)
foreach (Animal preyAnimal in this.prey)
if (preyAnimal.prefabGO == animal.prefabGO)
this.preyInArea.Add(animal);
(yes it is legal in C# to leave off the brackets if there is only one line of code in block)
This is only a theory, but if everything is cached I would think it would run pretty fast. The key is you are comparing cached prefab GameObjects, NOT the GameObject of the instance, which will not match since it is a different object in memorry.
Just food for thought. I don’t really know how you are getting things “in the area”, or what you plan on doing with this list of “preyInArea”…