Since Strings are not good for memory what else i can use to make sure which gameobject collided with the trigger?
This is how i use it now:
function OnTriggerEnter (col : Collider) {
if(col.name == "NameOfObject"){
}
}
Since Strings are not good for memory what else i can use to make sure which gameobject collided with the trigger?
This is how i use it now:
function OnTriggerEnter (col : Collider) {
if(col.name == "NameOfObject"){
}
}
Add a component with a public reference of your collider ( the one ho enters the trigger ) on the editor drag your collider into the component gameObject field.
You can also get the reference OnStart().
hm since i only need string comparison in Ontrigger that is not too memory heavy? right?
Modern runtimes like Mono generally use interned strings, which means most string values only ever occur once in any application. If you do something like:-
if (name == "Object1") { ... }
…then it will only use up extra memory if the string “Object1” does not already exist in the app. Generally, this kind of statement will just compare two pointers to see if they point to the same string value in memory. In any case, you can more or less ignore the memory footprint of strings unless you have an enormous number of them or you know you are very short of space (even an iPhone has at least 128MB of RAM - that’s a lot of string data).
On a sidenote, if you want strict comparison, it is recommanded to use the string submethod “Equals” to compare strings in C#.
It is slightly faster than stringA == stringB.
Your example contradicts what Andeeee stated. But he’s right, you’re not. You have not tested your own example at all: the output is True True.
In fact, even when the two strings are not interned, (s1 == s2) would still be true, as the string ‘==’ operator is implemented using the string.Equals function (source: String.Equality(String, String) Operator (System) | Microsoft Learn).
I’m sorry, but the use of " == " on cached string values did return me several unexpected statements …
That’s a fact. I don’t know why, but that’s a fact.
Plus if you search on Google, you’ll find numerous threads where people are saying the same as me (as I actually used them to confirm this post).
In the end, let’s do whatever compiles, heh ?
edit : for the example, I took it from another thread in another forum, as I can’t actually have access to my Mac. Sorry if the result is not confirmed. Deleted it.
edit 2 : a possible explanation to why " == " returned me false may be because I used it crossing over 2 classes. Anyway, I believe Equals is still safer for strict value comparison (I don’t think it without a purpose : I was forced to use that method in order to make my comparisons work all the time)