- Home /
Help me with Writing this line of Code neat and clean
Is there possible to write this if condition shortly?
if (gameObject.tag == "1" || gameObject.tag == "2" || gameObject.tag == "3")
{
//Tags goes up to 100.
}
Answer by AbandonedCrypt · Mar 08, 2021 at 09:48 AM
To be honest if your tag goes up to 100 and you want to check for each of them you really have bad architecture.
For your specific question, if your tags are RELIABLY integers, you could do this:
int tag = int.Parse(gameObject.tag);
if(tag <= 100 && tag > 0)
{
//do whatever
}
but that's TERRIBLE in all regards.
You are way better off rethinking your structure. You could simply give the same tag to every object that should respond to the query. Without any information about what you are trying to do in your game and how its structured we cant help you at all
@AbandonedCrypt I agree with u But i cannot find a better way. What i am trying to do actually ; i am moving the camera with touchscreen. And i have Sprites which i can move them with drag and drop. When i try to drag them also Camera moves. So for prevent moving camera while dragging i found that solution. works but i know not the best way.
private void OnMouseDown()
{
if(gameObject.tag == "1" || gameObject.tag == "2" || gameObject.tag == "3")
{
hitsprite = true;
}
}
//Another Script
void Update()
{
if (MoveObjectsManager.hitsprite)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
hit_position = Input.mousePosition;
camera_position = transform.position;
}
if (Input.GetMouseButton(0))
{
current_position = Input.mousePosition;
LeftMouseDrag();
}
}
void LeftMouseDrag()
{
current_position.z = hit_position.z = camera_position.y;
Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
direction = direction * -1;
Vector3 position = camera_position + direction;
position.y = Mathf.Clamp(position.y, -0.3084111f, -0.3084111f);
position.x = Mathf.Clamp(position.x, 0.16f, 50);
transform.position = position;
}
@AbandonedCrypt @Ercova Quite a bad approach here. You mustn't parse tags like so as they are strings. It will throw FormatException as soon as it meets any string. To avoid that use int.TryParse as it handles the correct format.
int tag;
int.TryParse(gameObject.tag, tag);
if(tag <= 100 && tag > 0)
{
//do whatever
}
Int.TryParse returns a bool and takes an out int
parameter
https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0
Jup, thanks, just did copy and paste, and forget to adjust. Appreciate your comment
Answer by Jopa-Przemyslaw · Mar 08, 2021 at 01:06 PM
Not to write hundred of numbers, maybe you can create an array of strings and then loop the tags.
string[] array = new string[100];
for(int i=0; i<100; i++)
{
array[0] = (i+1).ToString();
}
foreach(string s in array)
{
if(gameobject.tag.Contains(s))
{
//check that 100 tags
}
}
Answer by Alanoso15 · Mar 08, 2021 at 01:39 PM
Can't you use a switch case? In unity switch cases are slightly more efficient and work pretty much the way you want.
switch (gameObject.tag)
{
case "1":
//funi tag go 100
break;
case "2":
//funi tag go 100
break;
case "3":
//funi tag go 100
break;
default:
//funi tag doesn't go 100 :(
break;
}
Probably not much of an upgrade, though
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to clear TreeInstance-list? 1 Answer
Renderer on object disabled after level reload 1 Answer
Best way to make this code shorter? 2 Answers