Posts

How to color a cube object with different color in different side? Unity -- coloring Object

 How to color a cube object with different color in different side? Unity -- coloring Object more details. See Help Wanted - Shader Graph Apply Different colours onto different sides of the model - Unity Forum

How to fetch the object which the object collides with? Unity -- collision

 How to fetch the object which the object collides with? Unity -- collision [Ans] collision.gameObject.name; public void OnCollisionEnter(Collision collision)     {                    Debug.Log("collision name:" + collision.gameObject.name);              } Step1: make a collision event with the step in the following link How to do somethings after collision between two objects? -- Unity -- Collision (programmingunitycsharp.blogspot.com) (my article) Step2: In Script, write the line Debug.Log("collision name:" + collision.gameObject.name); 

How to do somethings after collision between two objects? -- Unity -- Collision

Image
 How to do somethings after collision between two objects? -- Unity -- Collision [Ans] OnCollisionEnter or OnTriggerEnter func. Suppose that when ACube object collides with Ground_Cube object, it will trigger event. Do following steps. Step1:  In Unity, add collider as component in ACube. Step2: In Unity, add collider as component in Ground_Cube. Step3: In Unity, add Rigidbody as component in ACube. Step4: Great Importance!!! Check the setting in Rigidbody in Ground_Cube,  collider in ACube and collider in Ground_Cube. Step5: In Script, override the OnCollisionEnter func. public void OnCollisionEnter(Collision collision) { //Do something } [NOTE] Great Importance!!! (1)Unity is a case-insensitive language. i.e. In Unity, uppercase and lowercase letter are different. onCollisionEnter is not accepted. If you spell it wrong, the func. will be recongized as your own func. If you do so, compiler will not throw error, but it can not work. (2)OnCollisionEnter func. must take 1 p...

How to fetch value from other script through code in Unity? -- Unity -- fetch script

How to fetch value from other script  through code in Unity? -- Unity -- fetch script [Ans] target script->destination script If I wanna fetch a value and store it into destination Script from target destination. I have to drag GameObject which is attached to target script to destination script. Step1: write a target script. Step2: write a destionation script. Step3: drag the component which attach the target script into the destination script. e.g. If I wanna change TextListScript.num to 20 in other Script, I will do following. In destination Script ButtonEventScript: public class ButtonEventScript { public TextListScript MyScript; MyScript.num=20; } In target Script  TextListScript: public class TextListScript { public int  num=0; } And Assuming that obj is attached to TextScript. I have to drag TextScript into MyScript field. That's done.

How to use Virtual Camera to follow a GameObject

 How to use Virtual Camera to follow a GameObject Way1:Cinemachine Virtual Camera package (1)Install cinemachine virtual camera package (2)drag object  to the properties "follow"

How to Create Button in Unity with code?

 How to Create Button in Unity with code? Way1:Use Instantiate function. Create Button  in Unity manually. Then drag it to a variable. After that, write the line such as Instantiate(obj); P.S. (1)I can not find way to set both the width and the height of the button now in this way. Way2:GUILayout.Button GUILayout is a class that  can create UI Object. P.S. (1) GUILayout can be written in OnGUI function. When GUILayout was called in other function. There is an ArgumentException  before play mode. (2)void OnGUI()  is a built-in function. It is case-sensitive, so OnGui() was not accepted. When you override it, you will see the color of OnGUI is blue when you code in visual studio 2019. (3)When is OnGUI() invoked? it is invoked several times per frame (or per time when the event is triggered). (4)I can find way to set both the width and the height of the button now in this way. pass the width and height as argument when you call GUILayout.Button. more details on: U...