Using a lot of calls and if else statement, is there a better way to do this?

Hi there, I’m still trying to figure out what is the best practice when it comes to game scripting in unity.

The script I’ve done displays different text on UI when a gameobject is detected from raycast. As you can see below, it is pretty lengthy.

Is there a better way to do this? Any pointers or advice is much appreciated!

    if(myScript.selectedObject.isEnemy || (myScript.selectedObject.isEnemy && myScript.selectedObject.isDestroyable))
    {
    	displayText[1] = inGameMonologueScript.canKill;
    }
    else
    {
    	displayText[1] = inGameMonologueScript.notEnemy;
    } 

if(isScenarioScript && isScenarioScript.scenarioBegun)
{
       displayText = isScenarioScript.scenarios[isScenarioScript.currentScenario].scenarioDialogue;
}

That should be just fine. You said that there are a lot of calls so is this inside Update method? Because you maybe could call it after selectedObject has changed, no need to do something like that on every frame.

Other thing I noticed that your first if doesn’t really make sense. Later part of it will never affect anything as it will be only checked when isEnemy is false and if it’s false then the later part will also always be false.

Did you mean for it to be something like this, where if isEnemy is false then it checks if isDestroyable is true and only if both are false then else is called.

if(myScript.selectedObject.isEnemy || myScript.selectedObject.isDestroyable)

If you wanted to check that if isEnemy is true and isDestroyable is also true then it should be like this:

if(myScript.selectedObject.isEnemy && myScript.selectedObject.isDestroyable)