Am doing this:
if (previousObject.GetComponent() != null)
{
correctPos = previousObject.GetComponent();
}
But am getting the component twice. Is there a way to just check and have? Maybe is not that bad if I do this in awake or start, but it would be useful to know.
2 Answers
2
Collider component = previousObject.GetComponent();
if (component != null)
{
correctPos = component;
}
[EDITED] Removed incorrect part of the answer mentionning the “??” operator. see @Hellium comment below.
Hello.
Answer is not 100% correct. Unamrked until its modifyed.
Hello. Answer is not 100% correct. Unamrked until its modifyed.
– tormentoarmagedoomThis is just basic programming. If you want to use an equation a few times, compute it once and put it in a variable. Suppose x can't be more than y2+7. Instead of writing "if(x>y2+7) x=y2+7" you can compute it ahead of time: int xMax=y2+7; if(x>xMax) x=xMax;
– Owen-Reynolds