Posts

How to quit application through script in Unity? -- Unity

 How to quit application through script in Unity? -- Unity Stop running  game at runtime. Debug.Break(); Close the Unity Application. EditorApplication.Exit(0);  Quit the deployed game/App. Application.Quit(); source: unity3d - How can i stop unity from script? - Stack Overflow

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...

DrawDefaultInspector()

 DrawDefaultInspector(); syntax:  DrawDefaultInspector();

SendMessage To Other GameObject in Unity -- SendMessage v.s. SendMessageTorward v.s. BroadcastMessage in Unity

 SendMessage To Other GameObject in Unity --  SendMessage v.s. SendMessageTorward v.s. BroadcastMessage in Unity introduction: Do you remember about GameObject in Unity. You can create lots of objects in Unity.

how to install a package in unity - unity

how to install a package in unity - unity  (1)open unity (2)select menu bar (Window->package manager) and a window which title is "package manager" pops up. (3)select menu bar (package registry) (4)type package's name that you want to download in search line. (5)check the information and install it. Resource: more details on: 10.unity攝影機跟隨功能(Cinemachine) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 (ithome.com.tw)

output message to Unity Console - Debug.Log() v.s. Debug.LogWarning() v.s. Debug.LogError() v.s. print()

Image
 output message to Unity Console - Debug.Log() v.s. Debug.LogWarning() v.s. Debug.LogError() v.s. print() usage and description: 1.Debug.Log(string message) The message will be printed in Unity Console. You can see it in Unity Console with General mode. 2.Debug.LogWarning(string message) The message was considered as a warning. Then the message will be printed in Unity Console. You can see it in Unity Console with Warning mode. When you see it you will see a warning figure(similar to a yellow triangle). 3.Debug.LogError(string message) The message was considered as a Error. Then the message will be printed in Unity Console. You can see it in Unity Console with Error mode. When you see it you will see a Error figure(similar to a red octagon). 4.print(string message) have same result with using  Debug.Log(string message). my blogger can explain that little difference  between Debug.Log() and print() Debug.Log() v.s. print() (programmingunitycsharp.blogspot.com) screenshot: ...

Debug.Log() v.s. print()

Image
 Debug.Log() v.s. print()  difference bewtween Debug.Log() and print(): In fact, they are identical. print() is a simple encapsulation of Debug.Log() According to the following screenshot. We can know Debug.Log() is in the class UnityEngine. And print() is in the class UnityEngine.Monobehaviour. screenshot:     code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PrintScript : MonoBehaviour {     [Header("Counter")]     public int Counter = 0;     [Header("Message")]     public string Message = "not initialized!";     // Start is called before the first frame update     void Start()     {              }     // Update is called once per frame     void FixedUpdate()     {         ChangeMessage();     }     private void ChangeMessage()     { ...

usage of Renderer.enabled

 usage of Renderer.enabled usage: An attribute which makes the rendered 3D object is visible or not. If it is set to false, the object will be set to be invisible. You can hide the gameObject with the following code: Render rend=this. gameObject.GetCompoent<Renderer>();  rend.enabled=false; word definition Renderer: definition: A renderer is what  makes an object appear on the screen . (from Unity documentation) Render: definition to cause  someone or something to be in a particular state. (from Oxford Cambridge) -er: suffix -er mean a person who does it. source: more details on   Unity - Scripting API: Renderer.enabled (unity3d.com)

GameObject.SetActive() v.s. GameObject.active

 GameObject.SetActive() v.s. GameObject.active usage: attribute active of a GameObject checks whether  the GameObject is active or not. method SetActive(bool b) of GameObject  is a method that sets the attribute of GameObject active is b. When you use GameObject.SetActive (false) or GameObject.active=false to deactivate the GameObject, then OnDisable() event will be triggered. After that, the GameObject.active will set to be false and  GameObject will be deactive. When you use GameObject.SetActive(true) or GameObject.active=true to activate the GameObject, then OnEnable() event will be triggered. After that, the GameObject.active will set to be true and  GameObject will be active. I prove it on my script. Unity Version: 2020.3.12f1.590.2 Personal NOTE:  When the GameObject is deactive, you can  NOT  acess it and its compoent including its  BoxCollider , Script which is attcahed to GameObject and transform etc. When the GameObject is deactiv...

Unity-Why can not change the view of main Camera into a position of the GameObject?

Unity-Why can not change the view  of main Camera into a position of the GameObject? A:For change the position  of main camera. You should (1)select the main camera with Camera class using the following code: Camera cam=Camera.main; (2)change the position.  Notice that when you change the localPosition (position which is relative to the parent GameObject) You should add it to fix the position. Here is my code: cam = Camera.main; offset=Unitychan_GameObject.transform.localPosition+UnitychanFace_GameObject.transform.localPosition+this.transform.localPosition;  cam.transform.localPosition = UnitychanEyes_GameObject.transform.localPosition + offset; P.S. localPosition  is a Position which is relative to parent GameObject.

Reseason about the fact that OnCollisionEnter event was not triggered when a GameObject touched the other GameObject.

Reseason about  the fact that  OnCollisionEnter event was not triggered when a GameObject touched the other GameObject. Q:I stuck on a problem. I  try to develop a new project with Unity. Sometimes,  a gameObject  named unitychan did not pass throught the wall which is left to it the first time it touched the wall. But unitychan passed through the wall which is left to it the second time it touched the wall then I saw uniychan dropping. Sometimes, uniychan passed through the wall which is back to it the first time it touched the wall. I take a look at the the setting of the GameObject in inspector and I ensured that two components(BoxCollider) of two GameObject were attatched. I stuck it on the similar problem with the third time I looked at it for 4 days. I finally solved it on third time (at 2021/08/05 p.m. 3:xx). What happened? P.S. PlayerControl.cs script was attached on unitychan GameObject. The wall was tagged with "Wall_Tag". The thickness of the wall is ...

Unity 鍵盤偵測時機(對新手很重要,建議看)

Unity 鍵盤偵測時機(對新手很重要,建議看) [description] 哥最近遇到很令人匪夷所思的問題,但我目前解決了。花了我幾天幾次的思考終於意外解決了。 我要提醒大家這個微小細節,微小到你不容易察覺有此概念。 我的問題是,當我剛開始執行時鍵盤按鈕是有作用的,但當我選擇看Console的輸出訊息我按鍵盤時沒任何作用。當我又選擇Project模式時他又有作用。 所以我合理推測,鍵盤偵測 只有在Project模式下會有作用 , 在Console模式下沒作用 。 我們下次見,掰掰。

Awake() v.s. Start() v.s. Update() v.s. FixedUpdate() v.s. LateUpdate()

Image
Awake() v.s. Start() v.s. Update() v.s. FixedUpdate()  Update() , FixedUpdate() , LateUpdate() 為不斷執行的函數 Awake(),Start()為一次執行的函數 細節稍後做解釋: 流程: 大致如下 Awake()->Start()->FixedUpdate() Awake()->Start()->Update()->LateUpdate() 函數介紹: 我只說正常情況下,所謂的正常情況是沒有執行續中斷或例外被拋出或該物件被設定為enable或disable等造成程式碼無法以正常程序執行。 程式語言C/C++/C#管理程式中函數呼叫的方式很深,用到堆疊概念,若不懂得可以去google一下或修C++之類的課程,我稍微講解一下。若沒興趣可以跳過,直接往下看。 程式語言C/C++/C#管理函數呼叫的方式 (programmingconception.blogspot.com) 執行續概念很深,用到CPU的底層知識,若不懂得可以去google一下或修作業系統之類的課程,我稍微講解一下。若沒興趣可以跳過,直接往下看。 Thread(執行續) (programmingconception.blogspot.com) Awake(): void Awake(){//一次性程式碼} 當該腳本(Script)被執行時,Awake()會先被呼叫 Start(): void Start(){//一次性程式碼} 當該腳本(Script)被執行時,Awake()會先被呼叫,再呼叫Start() Update(): void Update(){//無限執行程式碼} 當該腳本(Script)被執行時,Start()被呼叫後,Update()會被呼叫,當執行完LateUpate()函數後過一禎(per frame),Update()又會被呼叫 FixedUpdate(): void FixedUpdate(){//無限執行程式碼} 當該腳本(Script)被執行時,Start()執行完,每過timeStep秒FixedUpdate()會被呼叫一次 LateUpdate(): void LateUpdate(){//無限執行程式碼} 當該腳本(Script)被執行時,Up...

實用的呼叫函式-Invoke() vs InvokeRepeating()

實用的呼叫函式-Invoke() vs InvokeRepeating() Unity: Version: 2020.3.12f1.590.2 Personal Revision: 2020.3/release b3b2c6512326 Built: Tue, 08 Jun 2021 08:46:44 GMT 1.Invoke: syntax: Invoke(string MethodName,float t) description: Invoke the method MethodName() after t seconds. [Note] (1)Notice that "MethodName" is considered as MethodName() in compiler. (2)Notice that because of (1), MethodName must be a string, so it must be quotated such as "MethodName". (3)Notice that because of (1), the rule of naming of MethodName is almost same as the rule of naming of  a variable in C#. i.e. it only can contain a uppercase or lowercase or _ or digits in the string method name. It can not contain a bracket or other special character. (4)Notice that if t is set to zero, the MethodName() will be called in the  beginning of then next Update() cycle. [Review] Debug.Log("a") means that a will be output in Unity Console. more details on the website: For Unity beginner with C# synta...

For Unity beginner with C# syntax - part1 - Debug.Log();

Image
 For Unity beginner with C# syntax - part1 - Debug.Log(); syntax: Debug.Log(string s); description: string s will be outputed in Unity Console. common issue: Q:Why did not see string s in Console? A: Check if the string s is outputed in Console or not. 1.Maybe you just did not select the output mode. It will be outputed but you can not see it. output mode was selected. output mode was NOT selected. but the message was outputed. If it is not outputed. 2.Check the script,  check the script is attached on the object. If the script is not attached, the script will not be executed. you can check it through Hierarchy -> object you select ->Inspector  Logic error 3.Check that the Debug.Log(s); was called. I recommend that test a script at beginning. such as  void Start() { Debug.Log("Hello Unity"); } ByeBye~~