Posts

Showing posts with the label learn

Changing Image in unity by C# script

 There are two case when you want to change image one is UI image and other is a sprite in scene so in this post we will take a look at both cases how to change images. Changing UI Image There are very few steps to change UI image scripts First create a UI image gameObject in scene whose image we want to change create a script and attach with an other empty gameObject and open script In script declare a public GameObject for referencing changing image and a declare a private Image variable for refencing Image component in this gameObject also declare a public Sprite for which image that we want in replacement. using   System . Collections ; using   System . Collections . Generic ; using   UnityEngine ; using   UnityEngine . UI ; public   class   test  :  MonoBehaviour {      public   GameObject  image_object;      public   Sprite  mySprite;      private   Ima...

Finding a GameObject by C# script in unity

 There are main three ways of finding a gameObject in scene by script GameObject.Find() mehod You can pass string name in it and it will then search for that name in scene gameObject and return very first name which exactly match with it. using   System . Collections ; using   System . Collections . Generic ; using   UnityEngine ; public   class   test  :  MonoBehaviour {      private   GameObject  player;      // Start is called before the first frame update      void   Start ()     {          player  =  GameObject . Find ( "main_character" );          Debug . Log ( player );     } } In this example code we have declare a private GameObject and then we assign it by calling find() method. if there is exact ...

SerializeField and HideInInspector in unity in C# script

You can declare a variable as private or public according to you if you don't want to access variable outside of class then declare as private and want to access in other classes then declare public. Unity have two attribute for these variables lets take a look  [SerializeField]  Whenever you declare a private variable it is only accessible inside its class and variable can not be shown in inspector window in editor. So if you want to change value of this private variable you have to change inside script.      private   int  num =  5 ; this variable "num" will not show in inspector window.  If we want to show private variable in inspector window then we can use SerializeField attribute for example      [ SerializeField ]    private   int  num =  5 ; next declared variable after SerializeField attribute will be visible in inspector and we can change its value just by changing in inspector inst...

Activating and deactivating gameObjects in unity by C# script

Sometimes it is very useful to active or deactivate a gameObject by script. For this gameObject class have a called SetActive(). You can pass true or false in to active or deactivate For activating gameObject by script first declare a gameObject and then drag and drop in inspector or use Find method for referencing gameObject. then use gameObject.SetActive(true) when you want to visible (activate) gameObject.SetActive(false) when want to deactivate gameObject. if gameObject is parent object then its all child object will also deactivaate. using   System . Collections ; using   System . Collections . Generic ; using   UnityEngine ; public   class   test  :  MonoBehaviour {      public   GameObject  my_object;      // Start is called before the first frame update      void   Start ()     {      ...

how do I access variable or call a function from other script in unity

Image
 Accessing and modifying variable and calling from other script is quit simple. So in this post we access variable from other script and modify and call it. so lets get started.  Lets create an empty scene in your project. In this scene create two empty game object and named them "variable and function script" and "test  script". Now create two scripts one in which we declare variable and function named as "variable_function" and second by which we access these variable and call function named as "test".  Drag and drop variable_function script in "variable and function script" gameObject and test script in "test script" gameObject. You can use any name of gameObject and script these names are just for reference so you can understand which gameObject and scripts I am talking about. variable_function script using   System . Collections ; using   System . Collections . Generic ; using   UnityEngine ; public   class   variable_fun...

Scene Visibility in unity editor

Image
You can hide or show gameObjects by Unity scene visiblity it is very useful in creating complex scenes where it is difficult to see specific gameObjects. In the above image you will find an icon look like eye at very beginning of each gameObject in hierarchy window. You can hide or GameObject in scene view by by just clicking on that eye. if you click on a parent gameObject to hide it will also hide its all child object. You will see three type eye  1. One which is fully visible eye that show GameObject is visible. 2. Second in which eye is crossed that show gameObject is hided in scene view. 3. Third in which there is  a point at lower end of eye which means one or more child gameObject is hided.  Scene View Control Bar You can control your game audio and lighting enabled and various other options of scene view. These controls affect only game in editor and does not affect build game. Now we will discus all menus one by one Draw Mode Menu First one drop down is used how ...

Grid snapping in unity editor

Image
 There are virtual grid in the editor's Scene view that help you to align GameObjects snapping(moving) to the nearest grid You can change this grid to any axis x,y,or z.  Aligning (Moving) to the grid GameObject can be move to the single axis or along all axis to the closest grid. To move a gameObject to closest grid open Grid and snap window by either method From Main unity menu, Choose Edit>Grid and Snapping Setting From the grid visible in scene view menu open the overflow menu and choose Edit Grid and Snap Setting and you can reset all grid settings by just clicking reset as shown in below image. in grid and snap setting  you will find Align selection sector click on x, y, or z button or to all axis or you can did the same work by selecting the gameobject and pressing Ctrl + \ key together for windows and command+\ key for macOS. You can use increment Snap Section to modify how much you want to move and along which axis. Showing and Hiding Grid lines By toggling th...

Positioning GameObjects in unity editor

Image
 You can change transform component of any gameobjects by manipulating mouse on any Gizmo axis or you can change values of transform component in inspector window. transform has the following tools. let's take a look  Move tool You can select move tool in toolbar or can select by pressing hotkey W.  At the center of Move Gizmo there are three small squares by dragging these square you can move gameObject along a plane and third axis remains constant. if you press shift key while dragging Move Gizmo squares will change to flat square. This flat square indicates that you can move gameObject in the direction scene view camera is facing.  For moving  to particular axis just drag any one of the axis that you want to change. Rotation tool While you select rotate tool or press hotkey E you will see spheres around gameObject and you can change rotation by dragging this sphere Gizmo. As with the Move Gizmo, the last axis you changed will be colored yellow. Think of the r...

navigation and picking tools of Scene View in unity editor

Image
The Scene View The Scene view is your interactive world that we create. We can use scene view to select and position camera, character, lights and other gameObjects. Dealing with gameObjects is first skill that you learn. Scene View Navigation The Scene view has many navigation to move quickly around scene and these are following Scene Gizmo Scene Gizmo appears at the top right corner in scene view. It has conical arms with label x, y, and z. You can toggle perspective view on or off   In 2D mode Gizmo does not appear because you only have xy plane to navigate. Moving , orbiting and zooming in scene view For navigation in scene view moving, orbiting and zooming are key operations. Unity provide many tools for it. 1. Arrow Movement -  You can use arrow key to move around scene view. Arrow up and down key are used for moving forward and backward as scene is and left and right arrow use to move left and right side. You can use shift key to move fast. 2. Hand tool - You can s...