Findgameobjectwithtag

broken image


Submission failed. For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time. In case anyone attempts this and it STILL doesn't work, let me recommend what worked for me. I was attempting to get an initiative value out of a Timeline script on my Units in a turn-based tactics game. 23 minutes to read; In this article. Volume 29 Number 8. Unity: Developing Your First Game with Unity and C#. As a software architect, I've written many systems, reverse-­engineered native code malware, and generally could figure things out on the code side. FindWithTag 和 FindGameObjectWthTag. 在Unity的学习中,发现了 FindWithTag 和 FindGameObjectWithTag 这个两个方法做的都是相同的事情,但是在Unity的文档中只给出了 FindWithTag 的说明,没有 FindGameObjectWithTag 的解释。.

Return FindGameObjectWithTag (tag); public void SendMessageUpwards ( string methodName, SendMessageOptions options ) SendMessageUpwards ( methodName, null, options ).

Finding and referencing a game object properly in Unity3D is one of the most asked questions for those who are new to Unity3D. In this article, I will write about referencing game objects, finding them according to their names, tags, and types. But let me give you the answer to the question at the title briefly.

Findgameobjectwithtag inactive

In Unity3D, we can find game objects according to their names, tags, and types. For these purposes, we use the following methods respectively: GameObject.Find( ), GameObject.FindWithTag( ) and Object.FindObjectOfType(). These methods will return only one game object. Additionally, it is also possible to find all the game objects that have the same tag or are in the same type, using GameObject.FindGameObjectsWithTag( ) and Object.FindObjectsOfType( ). These methods will return arrays of game objects.

Contents

  • Referencing game objects in Unity3D

Referencing game objects in Unity3D

What is referencing?

Most of the time, we want to access other game objects and their components. Hence, we can gather information about them when it is required, send information to them, control their behaviors, or execute a method that is in a script that is attached to them. In these cases, we need an address(or let's say a phone number) of the game object and thus, we can dial whenever we want. This is called referencing.

While developing games in Unity3D, we always reference other game objects or components. Therefore, this is a fundamental topic you should understand before you go further.

How to create references to game objects or components

First of all, we have to declare variables that will store the address of the game objects or components. And hence, whenever we would like to access the properties of these game objects or components, we use these variables.

In the code above, we declared two variables in type GameObject but we have not assigned any object them yet. You may consider this as if we are reserving empty rows in an address book that we will fill them out later.

To reference these variables, we have a couple of options. We can search the game object that we want to assign using built-in methods that are included in Unity. We will see this option in later sections of this article. Another option is to assign relevant game objects directly in the Unity editor. But to do this we have to declare the objects either public or private with [SerializeField] attribute.

In the code above, the first variable is declared as private(you can put the private keyword in front of it as well if you want), the second variable is declared as public and the third variable is declared as private with [SerializeField] attribute. [SerializeField] attribute makes this variable visible in the editor but still inaccessible from other scripts.

Now, you can drag and drop the game objects, that you would like to assign, to the slots that are visible in the inspector.

If you would like to create references to the components that are associated with the game objects, you need to declare variables that are in the type of that component.

How to find game objects by their names

As I mentioned above, we can create references to game objects and components by searching and finding them using built-in methods in Unity. This is useful especially when you want to stay the variable private or when you want to access an object that is created during runtime.

In order to search for a game object that has a specific name in the scene, we have to use the method GameObject.Find( ). It takes a string parameter. And this parameter is the name of the game object that we want to find.

In the code above, we created a reference for the game object that has the name 'Sphere'.

If you would like to access a component that is attached to this game object, you should create a reference for that component. For instance, the following creates a reference for a rigidbody component that is attached to this game object, and hence you can access the properties of this component.

Finding a game object that has a specific tag

In addition to finding an object by its name, we can also find it by its tag which we are able to determine objects in the scene.

To find a game object that has a specific tag, we use the method GameObject.FindWithTag( ). This method takes a string parameter and searches for it (There is one more method that does the same job. You can also use GameObject.FindGameObjectWithTag( ) for the same purpose).

A tag can be used either for only one object or multiple objects. If there is more than one object that has the same tag, this method returns only one of them.

Getting the array of all game objects that have the same tag

If there are multiple objects that have the same tag and we want to get them all, we need to use the method GameObject.FindGameObjectsWithTag( ). Likewise, this method also takes a string parameter and returns an array of all game objects. Therefore, we have to declare an array that will store the game objects.

The following returns and assigns all objects that have the tag 'Cube'.

Finding a game object that matches a specific type

We can also search and find a game object that matches a specific type. In other words, for instance, we can get a game object or component that has a specific component. To do this, we use the method Object.FindObjectOfType( ). This method also returns only one of the objects.

Findgameobjectswithtag performance

The following one finds a light component and creates a reference to it.

Getting the array of all game objects that match a specific type

If there are multiple game objects that match a specific type, we can find all of them and create references in an array. For this purpose, we use the method Object.FindObjectsOfType( ). This is an example of how we use it:

How to find a child game object

In order to find a child object and create a reference to that child object we can use the method Transform.Find( ). This method takes a string parameter and returns the child game object that the name matches this parameter.

Assume that there is a game object in the scene that has a tag 'Player'. And also assume that this player object has a child game object which has the name 'Gun'. If we want to create a reference for the 'Gun' object, we may do this as the following:

This is useful especially if there are multiple objects that has the same name under different objects.

Further remarks

In this article, we see different ways of how we reference game objects in Unity3D. Here, I need to warn you that searching and finding methods are extremely slow and you should avoid using them in Update, FixedUpdate, or LateUpdate methods, if possible. We generally use them in Start or Awake methods. You may also consider creating references in Singleton's to improve performances. We have various tutorials about the Unity3D Engine that you can see in this link.

-->

Unity is a game engine that enables you to develop games in C#. This walkthrough shows how to get started developing and debugging Unity games using Visual Studio for Mac and the Visual Studio for Mac Tools for Unity extension alongside the Unity environment.

Visual Studio for Mac Tools for Unity is a free extension, installed with Visual Studio for Mac. It enables Unity developers to take advantage of the productivity features of Visual Studio for Mac, including excellent IntelliSense support, debugging features, and more.

Objectives

  • Learn about Unity development with Visual Studio for Mac
Findgameobjectwithtag

Prerequisites

  • Visual Studio for Mac (https://www.visualstudio.com/vs/mac)
  • Unity 5.6.1 Personal Edition or higher (https://store.unity.com, requires a unity.com account to run)

Intended Audience

This lab is intended for developers who are familiar with C#, although deep experience is not required.

Findgameobjectwithtag

Task 1: Creating a basic Unity project

Findgameobjectwithtag

  1. Launch Unity. Sign in if requested.

  2. Click New.

  3. Set the Project name to 'UnityLab' and select 3D. Click Create project.

  4. You're now looking at the default Unity interface. It has the scene hierarchy with game objects on the left, a 3D view of the blank scene shown in the middle, a project files pane on the bottom, and inspector and services on the right. Of course, there's a lot more to it than that, but those are few of the more important components.

  5. For developers new to Unity, everything that runs in your app will exist within the context of a scene. A scene file is a single file that contains all sorts of metadata about the resources used in the project for the current scene and its properties. When you package your app for a platform, the resulting app will end up being a collection of one or more scenes, plus any platform-dependent code you add. You can have as many scenes as desired in a project.

  6. The new scene just has a camera and a directional light in it. A scene requires a camera for anything to be visible and an Audio Listener for anything to be audible. These components are attached to a GameObject.

  7. Select the Main Camera object from the Hierarchy pane.

  8. Select the Inspector pane from the right side of the window to review its properties. Camera properties include transform information, background, projection type, field of view, and so on. An Audio Listener component was also added by default, which essentially renders scene audio from a virtual microphone attached to the camera.

  9. Select the Directional Light object. This provides light to the scene so that components like shaders know how to render objects.

  10. Use the Inspector to see that it includes common lighting properties including type, color, intensity, shadow type, and so on.

  11. It is important to point out that projects in Unity are a little different from their Visual Studio for Mac counterparts. In the Project tab on the bottom, right-click the Assets folder and select Reveal in Finder.

  12. Projects contain Assets, Library, ProjectSettings, and Temp folders as you can see. However, the only one that shows up in the interface is the Assets folder. The Library folder is the local cache for imported assets; it holds all metadata for assets. The ProjectSettings folder stores settings you can configure. The Temp folder is used for temporary files from Mono and Unity during the build process. There is also a solution file that you can open in Visual Studio for Mac (UnityLab.sln here).

  13. Close the Finder window and return to Unity.

  14. The Assets folder contains all your assets-art, code, audio, etc. It's empty now, but every single file you bring into your project goes here. This is always the top-level folder in the Unity Editor. But always add and remove files via the Unity interface (or Visual Studio for Mac) and never through the file system directly.

  15. The GameObject is central to development in Unity as almost everything derives from that type, including models, lights, particle systems, and so on. Add a new Cube object to the scene via the GameObject > 3D Object > Cube menu.

  16. Take a quick look at the properties of the new GameObject and see that it has a name, tag, layer, and transform. These properties are common to all GameObjects. In addition, several components were attached to the Cube to provide needed functionality including mesh filter, box collider, and renderer.

  17. Rename the Cube object, which has the name 'Cube' by default, to 'Enemy'. Make sure to press Enter to save the change. This will be the enemy cube in our simple game.

  18. Add another Cube object to the scene using the same process as above, and name this one 'Player'.

  19. Tag the player object 'Player' as well (see Tag drop-down control just under name field). We'll use this in the enemy script to help locate the player game object.

  20. In the Scene view, move the player object away from the enemy object along the Z axis using the mouse. You can move along the Z axis by selecting and dragging the cube by its red panel toward the blue line. Since the cube lives in 3D space, but can only be dragged in 2D each time, the axis on which you drag is especially important.

  21. Move the cube downward and to the right along the axis. This updates the Transform.Position property in the Inspector. Be sure to drag to a location similarly to what's shown here to make later steps easier in the lab.

  22. Now you can add some code to drive the enemy logic so that it pursues the player. Right-click the Assets folder in the Project window and select Create > C# Script.

  23. Name the new C# script 'EnemyAI'.

  24. To attach scripts to game objects drag the newly created script onto the Enemy object in the Hierarchy pane. Now that object will use behaviors from this script.

  25. Select File > Save Scenes to save the current scene. Name it 'MyScene'.

Task 2: Working with Visual Studio for Mac Tools for Unity

  1. The best way to edit C# code is to use Visual Studio for Mac. You can configure Unity to use Visual Studio for Mac as its default handler. Select Unity > Preferences.

  2. Select the External Tools tab. From the External Script Editor dropdown, select Browse and select Applications/Visual Studio.app. Alternatively, if there's already a Visual Studio option, just select that.

  3. Unity is now configured to use Visual Studio for Mac for script editing. Close the Unity Preferences dialog.

  4. Double-click EnemyAI.cs to open it in Visual Studio for Mac.

  5. The Visual Studio solution is straightforward. It contains an Assets folder (the same one from Finder) and the EnemyAI.cs script created earlier. In more sophisticated projects, the hierarchy will likely look different than what you see in Unity.

  6. EnemyAI.cs is open in the editor. The initial script just contains stubs for the Start and Update methods.

  7. Replace the initial enemy code with the code below.

  8. Take a quick look at the simple enemy behavior that is defined here. In the Start method, we get a reference to the player object (by its tag), as well as its transform. In the Update method, which is called every frame, the enemy will move towards the player object. The keywords and names use color coding to make it easier to understand the codebase in Visual Studio for Mac.

  9. Save the changes to the enemy script in Visual Studio for Mac.

Task 3: Debugging the Unity project

  1. Set a breakpoint on the first line of code in the Start method. You can either click in the editor margin at the target line or place cursor on the line and press F9.

  2. Click the Start Debugging button or press F5. This will build the project and attach it to Unity for debugging.

  3. Return to Unity and click the Run button to start the game.

  4. The breakpoint should be hit and you can now use the Visual Studio for Mac debugging tools.

  5. From the Locals window, locate the this pointer, which references an EnemyAI object. Expand the reference and see that you can browse the associated members like Speed.

  6. Remove the breakpoint from the Start method the same way it was added-by either clicking it in the margin or selecting the line and press F9.

  7. Press F10 to step over the first line of code that finds the Player game object using a tag as parameter.

  8. Hover the mouse cursor over the player variable within the code editor window to view its associated members. You can even expand the overlay to view child properties.

  9. Press F5 or press the Run button to continue execution. Return to Unity to see the enemy cube repeatedly approach the player cube. You may need to adjust the camera if it's not visible.

  10. Switch back to Visual Studio for Mac and set a breakpoint on the first line of the Update method. It should be hit immediately.

  11. Suppose the speed is too fast and we want to test the impact of the change without restarting the app. Locate the Speed variable within the Autos or Locals window and then change it to '10' and press Enter.

  12. Remove the breakpoint and press F5 to resume execution.

  13. Return to Unity to view the running application. The enemy cube is now moving at a fifth of the original speed.

  14. Stop the Unity app by clicking the Play button again.

  15. Return to Visual Studio for Mac. Stop the debugging session by clicking the Stop button. Portrait pro 2020.

Findgameobjectwithtag

In Unity3D, we can find game objects according to their names, tags, and types. For these purposes, we use the following methods respectively: GameObject.Find( ), GameObject.FindWithTag( ) and Object.FindObjectOfType(). These methods will return only one game object. Additionally, it is also possible to find all the game objects that have the same tag or are in the same type, using GameObject.FindGameObjectsWithTag( ) and Object.FindObjectsOfType( ). These methods will return arrays of game objects.

Contents

  • Referencing game objects in Unity3D

Referencing game objects in Unity3D

What is referencing?

Most of the time, we want to access other game objects and their components. Hence, we can gather information about them when it is required, send information to them, control their behaviors, or execute a method that is in a script that is attached to them. In these cases, we need an address(or let's say a phone number) of the game object and thus, we can dial whenever we want. This is called referencing.

While developing games in Unity3D, we always reference other game objects or components. Therefore, this is a fundamental topic you should understand before you go further.

How to create references to game objects or components

First of all, we have to declare variables that will store the address of the game objects or components. And hence, whenever we would like to access the properties of these game objects or components, we use these variables.

In the code above, we declared two variables in type GameObject but we have not assigned any object them yet. You may consider this as if we are reserving empty rows in an address book that we will fill them out later.

To reference these variables, we have a couple of options. We can search the game object that we want to assign using built-in methods that are included in Unity. We will see this option in later sections of this article. Another option is to assign relevant game objects directly in the Unity editor. But to do this we have to declare the objects either public or private with [SerializeField] attribute.

In the code above, the first variable is declared as private(you can put the private keyword in front of it as well if you want), the second variable is declared as public and the third variable is declared as private with [SerializeField] attribute. [SerializeField] attribute makes this variable visible in the editor but still inaccessible from other scripts.

Now, you can drag and drop the game objects, that you would like to assign, to the slots that are visible in the inspector.

If you would like to create references to the components that are associated with the game objects, you need to declare variables that are in the type of that component.

How to find game objects by their names

As I mentioned above, we can create references to game objects and components by searching and finding them using built-in methods in Unity. This is useful especially when you want to stay the variable private or when you want to access an object that is created during runtime.

In order to search for a game object that has a specific name in the scene, we have to use the method GameObject.Find( ). It takes a string parameter. And this parameter is the name of the game object that we want to find.

In the code above, we created a reference for the game object that has the name 'Sphere'.

If you would like to access a component that is attached to this game object, you should create a reference for that component. For instance, the following creates a reference for a rigidbody component that is attached to this game object, and hence you can access the properties of this component.

Finding a game object that has a specific tag

In addition to finding an object by its name, we can also find it by its tag which we are able to determine objects in the scene.

To find a game object that has a specific tag, we use the method GameObject.FindWithTag( ). This method takes a string parameter and searches for it (There is one more method that does the same job. You can also use GameObject.FindGameObjectWithTag( ) for the same purpose).

A tag can be used either for only one object or multiple objects. If there is more than one object that has the same tag, this method returns only one of them.

Getting the array of all game objects that have the same tag

If there are multiple objects that have the same tag and we want to get them all, we need to use the method GameObject.FindGameObjectsWithTag( ). Likewise, this method also takes a string parameter and returns an array of all game objects. Therefore, we have to declare an array that will store the game objects.

The following returns and assigns all objects that have the tag 'Cube'.

Finding a game object that matches a specific type

We can also search and find a game object that matches a specific type. In other words, for instance, we can get a game object or component that has a specific component. To do this, we use the method Object.FindObjectOfType( ). This method also returns only one of the objects.

The following one finds a light component and creates a reference to it.

Getting the array of all game objects that match a specific type

If there are multiple game objects that match a specific type, we can find all of them and create references in an array. For this purpose, we use the method Object.FindObjectsOfType( ). This is an example of how we use it:

How to find a child game object

In order to find a child object and create a reference to that child object we can use the method Transform.Find( ). This method takes a string parameter and returns the child game object that the name matches this parameter.

Assume that there is a game object in the scene that has a tag 'Player'. And also assume that this player object has a child game object which has the name 'Gun'. If we want to create a reference for the 'Gun' object, we may do this as the following:

This is useful especially if there are multiple objects that has the same name under different objects.

Further remarks

In this article, we see different ways of how we reference game objects in Unity3D. Here, I need to warn you that searching and finding methods are extremely slow and you should avoid using them in Update, FixedUpdate, or LateUpdate methods, if possible. We generally use them in Start or Awake methods. You may also consider creating references in Singleton's to improve performances. We have various tutorials about the Unity3D Engine that you can see in this link.

-->

Unity is a game engine that enables you to develop games in C#. This walkthrough shows how to get started developing and debugging Unity games using Visual Studio for Mac and the Visual Studio for Mac Tools for Unity extension alongside the Unity environment.

Visual Studio for Mac Tools for Unity is a free extension, installed with Visual Studio for Mac. It enables Unity developers to take advantage of the productivity features of Visual Studio for Mac, including excellent IntelliSense support, debugging features, and more.

Objectives

  • Learn about Unity development with Visual Studio for Mac

Prerequisites

  • Visual Studio for Mac (https://www.visualstudio.com/vs/mac)
  • Unity 5.6.1 Personal Edition or higher (https://store.unity.com, requires a unity.com account to run)

Intended Audience

This lab is intended for developers who are familiar with C#, although deep experience is not required.

Task 1: Creating a basic Unity project

Findgameobjectwithtag

  1. Launch Unity. Sign in if requested.

  2. Click New.

  3. Set the Project name to 'UnityLab' and select 3D. Click Create project.

  4. You're now looking at the default Unity interface. It has the scene hierarchy with game objects on the left, a 3D view of the blank scene shown in the middle, a project files pane on the bottom, and inspector and services on the right. Of course, there's a lot more to it than that, but those are few of the more important components.

  5. For developers new to Unity, everything that runs in your app will exist within the context of a scene. A scene file is a single file that contains all sorts of metadata about the resources used in the project for the current scene and its properties. When you package your app for a platform, the resulting app will end up being a collection of one or more scenes, plus any platform-dependent code you add. You can have as many scenes as desired in a project.

  6. The new scene just has a camera and a directional light in it. A scene requires a camera for anything to be visible and an Audio Listener for anything to be audible. These components are attached to a GameObject.

  7. Select the Main Camera object from the Hierarchy pane.

  8. Select the Inspector pane from the right side of the window to review its properties. Camera properties include transform information, background, projection type, field of view, and so on. An Audio Listener component was also added by default, which essentially renders scene audio from a virtual microphone attached to the camera.

  9. Select the Directional Light object. This provides light to the scene so that components like shaders know how to render objects.

  10. Use the Inspector to see that it includes common lighting properties including type, color, intensity, shadow type, and so on.

  11. It is important to point out that projects in Unity are a little different from their Visual Studio for Mac counterparts. In the Project tab on the bottom, right-click the Assets folder and select Reveal in Finder.

  12. Projects contain Assets, Library, ProjectSettings, and Temp folders as you can see. However, the only one that shows up in the interface is the Assets folder. The Library folder is the local cache for imported assets; it holds all metadata for assets. The ProjectSettings folder stores settings you can configure. The Temp folder is used for temporary files from Mono and Unity during the build process. There is also a solution file that you can open in Visual Studio for Mac (UnityLab.sln here).

  13. Close the Finder window and return to Unity.

  14. The Assets folder contains all your assets-art, code, audio, etc. It's empty now, but every single file you bring into your project goes here. This is always the top-level folder in the Unity Editor. But always add and remove files via the Unity interface (or Visual Studio for Mac) and never through the file system directly.

  15. The GameObject is central to development in Unity as almost everything derives from that type, including models, lights, particle systems, and so on. Add a new Cube object to the scene via the GameObject > 3D Object > Cube menu.

  16. Take a quick look at the properties of the new GameObject and see that it has a name, tag, layer, and transform. These properties are common to all GameObjects. In addition, several components were attached to the Cube to provide needed functionality including mesh filter, box collider, and renderer.

  17. Rename the Cube object, which has the name 'Cube' by default, to 'Enemy'. Make sure to press Enter to save the change. This will be the enemy cube in our simple game.

  18. Add another Cube object to the scene using the same process as above, and name this one 'Player'.

  19. Tag the player object 'Player' as well (see Tag drop-down control just under name field). We'll use this in the enemy script to help locate the player game object.

  20. In the Scene view, move the player object away from the enemy object along the Z axis using the mouse. You can move along the Z axis by selecting and dragging the cube by its red panel toward the blue line. Since the cube lives in 3D space, but can only be dragged in 2D each time, the axis on which you drag is especially important.

  21. Move the cube downward and to the right along the axis. This updates the Transform.Position property in the Inspector. Be sure to drag to a location similarly to what's shown here to make later steps easier in the lab.

  22. Now you can add some code to drive the enemy logic so that it pursues the player. Right-click the Assets folder in the Project window and select Create > C# Script.

  23. Name the new C# script 'EnemyAI'.

  24. To attach scripts to game objects drag the newly created script onto the Enemy object in the Hierarchy pane. Now that object will use behaviors from this script.

  25. Select File > Save Scenes to save the current scene. Name it 'MyScene'.

Task 2: Working with Visual Studio for Mac Tools for Unity

  1. The best way to edit C# code is to use Visual Studio for Mac. You can configure Unity to use Visual Studio for Mac as its default handler. Select Unity > Preferences.

  2. Select the External Tools tab. From the External Script Editor dropdown, select Browse and select Applications/Visual Studio.app. Alternatively, if there's already a Visual Studio option, just select that.

  3. Unity is now configured to use Visual Studio for Mac for script editing. Close the Unity Preferences dialog.

  4. Double-click EnemyAI.cs to open it in Visual Studio for Mac.

  5. The Visual Studio solution is straightforward. It contains an Assets folder (the same one from Finder) and the EnemyAI.cs script created earlier. In more sophisticated projects, the hierarchy will likely look different than what you see in Unity.

  6. EnemyAI.cs is open in the editor. The initial script just contains stubs for the Start and Update methods.

  7. Replace the initial enemy code with the code below.

  8. Take a quick look at the simple enemy behavior that is defined here. In the Start method, we get a reference to the player object (by its tag), as well as its transform. In the Update method, which is called every frame, the enemy will move towards the player object. The keywords and names use color coding to make it easier to understand the codebase in Visual Studio for Mac.

  9. Save the changes to the enemy script in Visual Studio for Mac.

Task 3: Debugging the Unity project

  1. Set a breakpoint on the first line of code in the Start method. You can either click in the editor margin at the target line or place cursor on the line and press F9.

  2. Click the Start Debugging button or press F5. This will build the project and attach it to Unity for debugging.

  3. Return to Unity and click the Run button to start the game.

  4. The breakpoint should be hit and you can now use the Visual Studio for Mac debugging tools.

  5. From the Locals window, locate the this pointer, which references an EnemyAI object. Expand the reference and see that you can browse the associated members like Speed.

  6. Remove the breakpoint from the Start method the same way it was added-by either clicking it in the margin or selecting the line and press F9.

  7. Press F10 to step over the first line of code that finds the Player game object using a tag as parameter.

  8. Hover the mouse cursor over the player variable within the code editor window to view its associated members. You can even expand the overlay to view child properties.

  9. Press F5 or press the Run button to continue execution. Return to Unity to see the enemy cube repeatedly approach the player cube. You may need to adjust the camera if it's not visible.

  10. Switch back to Visual Studio for Mac and set a breakpoint on the first line of the Update method. It should be hit immediately.

  11. Suppose the speed is too fast and we want to test the impact of the change without restarting the app. Locate the Speed variable within the Autos or Locals window and then change it to '10' and press Enter.

  12. Remove the breakpoint and press F5 to resume execution.

  13. Return to Unity to view the running application. The enemy cube is now moving at a fifth of the original speed.

  14. Stop the Unity app by clicking the Play button again.

  15. Return to Visual Studio for Mac. Stop the debugging session by clicking the Stop button. Portrait pro 2020.

Task 4: Exploring Unity features in Visual Studio for Mac

Findgameobjectswithtag

  1. Visual Studio for Mac provides quick access to Unity documentation within the code editor. Place the cursor somewhere on the Vector3 symbol within the Update method and press ⌘ Command + '.

  2. A new browser window opens to the documentation for Vector3. Close the browser window when satisfied.

  3. Visual Studio for Mac also provides some helpers to quickly create Unity behavior classes. From Solution Explorer, right-click Assets and select Add > New MonoBehaviour.

  4. The newly created class provides stubs for the Start and Update methods. After the closing brace of the Update method, start typing 'onmouseup'. As you type, notice that Visual Studio's IntelliSense quickly zeros in on the method you're planning to implement. Select it from the provided autocomplete list. It will fill out a method stub for you, including any parameters.

  5. Inside the OnMouseUp method, type 'base.' to see all of the base methods available to call. You can also explore the different overloads of each function using the paging option in the top-right corner of the IntelliSense flyout.

  6. Visual Studio for Mac also enables you to easily define new shaders. From Solution Explorer, right-click Assets and select Add > New Shader.

  7. The shader file format gets full color and font treatment to make it easier to read and understand.

  8. Return to Unity. You'll see that since Visual Studio for Mac works with the same project system, changes made in either place are automatically synchronized with the other. Now it's easy to always use the best tool for the task.

Summary

In this lab, you've learned how to get started creating a game with Unity and Visual Studio for Mac. See https://unity3d.com/learn to learn more about Unity.





broken image