Another idea to work something out

so i want to know if i can access the tag of the breakable blocks from the the level script and if possible can u tell me how and whats the difference between the used method and my method … thank you

Hi Hamza,

For those students that haven’t taken the course, or perhaps haven’t got to where you are, and don’t know what the used method you refer to is, could you explain?

1 Like

so i have a block script which is a component of the block game object … and i have a level script related to the level game object … when i start the game the block script starts and it calls the function that counts the number of blocks from the level script so what we did here is apply a if statement to the level.countbreakableobject()
but i want to know if there is a function that can help … what i want to do is access the block as a game object in general not access a certain block and see if it has a breakable tag on it so i can count it in … i want to know if there is a method to access a game object from another script !!

You could write one, using some of Unity’s API.

There’s a bit of a contradiction there, if you want to access a block in general then it would be an instance of a block, of which your scene will have many, if you want to access a tag then you’d want to access the tags of all of the blocks in the scene.

Again, you could create one.

So, to elaborate a little.

Unity’s API provides methods such as Object.FindObjectsOfType and GameObject.FindGameObjectsWithTag.

So, if you wanted to create a method within your Level class to find all of the blocks that had a specific tag, you could use something like this;

private int GetCountOfBreakableBlocks()
{
    return GameObject.FindGameObjectsWithTag("Breakable").Length;
}

Be aware that the tag is case-sensitive, so “Breakable” is not the same as “breakable”, which is why often using string references in this way is bad.

The use of any of the “Find” methods are not overly performant, so you want to restrict their usage as much as possible, for example, do not have this type of method call in an Update statement, or another method which is called repeatedly from an Update statement. Instead, call it once and cache the results.

On a performance note, at the moment the individual blocks will make a call to increment the count of breakable blocks, if you have 10 blocks, that’s 10 calls, if there are 200 blocks, 200 calls. The “Find” methods work by iterating through every GameObject (that is active/enabled) and perform the check against type or tag. If you had 1000 GameObjects in the scene the check would need to be performed against 1000 GameObjects.

Hope this helps. :slight_smile:


See also;

thats exactly was i was thinking of i just didnt know the GameObject.FindGameObjectsWithTag(“Breakable”).Length
i thought i had to search in the docs for a long time xD …
just one more question
WHAT IS APi

1 Like

If you Google just some of the words you think are relevant and precede them with “Unity”, for example;

“Unity find objects by tag”

You’ll get links to the Unity documentation in the top results, along with answers/examples - of course you can ask here too :slight_smile:

API = Application Programming Interface

1 Like

Privacy & Terms