Queen
Queen

Reputation: 185

best practice to show possible positions and put object on one of them Unity

I have a lot of Game Objects I need to Instantiate and put on scene on possible positions.

I have a list of rules where GameObject can be put by player (e.g. small box only at big box, sphere only on small box etc.)

I know I can put every rule into an "if", but I think it's not the most efficienty way. How should I keep a list of rules where can user put GameObjects? How my scripts should check and show possible positions? I will be thankful for any ideas how to start, to make it elegant and efficient.

Upvotes: 0

Views: 64

Answers (1)

Immorality
Immorality

Reputation: 2174

There are many ways to do this. What i would prefer is to keep everything organized and configurable per object (looking at the object oriënted programming structure).

I suggest defining per object where it can be placed. Here an example.

public yourClass
{
   public GameObject ObjectToPlace;
   public List<GameObject> ObjectToPlaceOnList;

   public bool CanObjectBePlaced(GameObject targetObjectToPlaceUpon)
   {
      if (ObjectToPlaceOnList.Contains(targetObjectToPlaceUpon))
      {
         return true;
      }

   return false;
   }
}

Your script may look different ofcourse, based on your current scripts.

Upvotes: 1

Related Questions