Hi, I'm new to coding, and I would like to know how I make it so I don't have to type the same code again and again to do something. This is what I want to "Shorten" : Code (csharp): void OnTriggerEnter (Collider other) { if (other.gameObject.CompareTag ("Green Rupee")) { other.gameObject.SetActive (false); } if (other.gameObject.CompareTag ("Blue Rupee")) { other.gameObject.SetActive (false); } if (other.gameObject.CompareTag ("Red Rupee")) { other.gameObject.SetActive (false); } if (other.gameObject.CompareTag ("Purple Rupee")) { other.gameObject.SetActive (false); } if (other.gameObject.CompareTag ("Silver Rupee")) { other.gameObject.SetActive (false); } if (other.gameObject.CompareTag ("Yellow Rupee")) { other.gameObject.SetActive (false); } } Any help is greatly appreciated!
https://forum.unity.com/threads/using-code-tags-properly.143875/ As far as your question goes, you could add a script to your rupee that you can get the value of that rupee from vs checking for all the possible color options.
Code (CSharp): ISet<string> Rupees = new HashSet<string> { "Blue Rupee", "Red Rupee", "Silver Rupee", "Purple Rupee", "Yellow Rupee" }; //... if (Rupees.Any(gameObject.CompareTag)) { gameObject.SetActive(false); }
I'm assuming he wants a value of the Rupee which is why OP had an if statement to compare which one was picked up.
Put the OnTriggerEnter method on the rupee instead. Assuming the player is the one triggering. Code (csharp): void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Player")) gameObject.SetActive(false); }
I agree with this the most. Why not create a "collectable" component script and attach that to anything you want to be collectable instead?
Yep, there are many ways to do this, but some of it depends on how much more you plan to do with this. Simply turning off the object isn't a big deal, but I'm assuming you'll have various values, maybe other pickups, etc that you'll need to have additional stuff to deal with. A pickup script on the object itself is one of the better ways to do this.
I'd agree on @KelsoMRK and just do it the other way around. This could be applied to anything that needs the same behaviour without caring about lots of different tags, it's easily extensible and maintainable since you do not need to adjust any tag collection, and you cannot forget to 'tag' a prefab / a go in general as this single comparison covers it all. However, if you need more behaviour in any way, like triggering certain behaviour for different kinds of 'rupees' or other items, you should follow the suggestion about the component anyway, which can eliminate the need for the tag in many situations completely. Also, if you feel like that's not what you want, you could start searching for patterns, i.e. (in it's simplest form) using the 'Contains', 'StartsWith', 'EndsWith' methods of strings. But honestly, this wouldn't be as reliable as the recommendation already given in this thread, and it's just as bad in regards to reliability, maintainability and extensibility as the other suggestions.