PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] How much cost GameObject.FindGameObjects takes in Unity Update function

 November 01, 2022     c#, performance, unity3d     No comments   

Issue

I'm making a game in which I want to player collect coins using a magnet here is the coins spawning code

// Spawn Coins In Air
void SpawnCoinsInAir ()
{
  float yPos = transform.position.y + 1f;
  float zPos = transform.position.z + 4f;
  for (int i = 0 ; i < 10 ; i += 1)
  {
    int line =  i == 0 ? 
    RandomsPlayerController.Instance.CurrentLineIdx 
    :
    Random.Range(0, RandomsPlayerController.Instance.Lines.Count);

    for (int j = 0 ; j < Random.Range(3,6) ; j += 1) 
    {
      zPos += j + 10f;
      var it = Instantiate(
       coinTransform,
       new Vector3(RandomsPlayerController.Instance.Lines [line].x,yPos , zPos),
        Quaternion.identity
      );

      // do random rotattion in y
      it.transform.DORotate (
        Utilities.VectorY (Vector3.zero, Random.Range(-360,360)),
        1f,
        RotateMode.FastBeyond360
      )
      .SetEase (Ease.InOutSine);

      Destroy (it, actionDuration + 1f);
    }
  }
}

Now I got all my coins in my game and I have to find each coin by its tag and collect them in the shortest distance.

I'm wondering how much cost it takes to find all coins in the Update function OR is there any way to do the same thing by keeping performance in mind?

Here is the code

private void UseMagnet ()
{
  // collect coins
  foreach (var coin in  GameObject.FindGameObjectsWithTag ("coin")) continue;
}

private void Update () => UseMagnet();

Thanks in Advance


Solution

In contrary to the generic Find, which you should never use if there is any other option, the FindGameObjectsWithTag as the name says uses a hashed (pre-indexed) tag which is quite optimized and not too expensive.

Of course there is still other ways to go which are even faster.

I would use a collection so the type itself can keep track of its own instances:

public class Coin : MonoBehaviour
{
    private static readonly HashSet<Coin> instances = new ();

    public static IEnumerable<Coin> Instances => instances;

    private void Awake()
    {
        // will automatically register itself when coming to live
        instances.Add(this);
    }

    private void OnDestroy()
    {
        // will automatically unregister itself when destroyed
        instances.Remove(this);
    }
}

Now you can simply iterate all coins via e.g.

foreach(var coin in Coin.Instances)
{
    // check if close enough for your magnet e.g.
    if(Vector3.Distance(coin.transform.position, player.transform.position) <= magnetRange)
    {
        //TODO: e.g. add points ?

        Destroy(coin.gameObject);
    } 
}


Answered By - derHugo
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing