Tuesday, May 30, 2017

Finding UI Button from Canvas Gameobject in Unity 5.6

So in this scene, I have canvas with two buttons as shown in the picture below
This restart button has a tag 'restart_tag' and exit with 'exit_tag' button...

And in this scene, I want to reference to my buttons using variabel in my script and this variabel is a private var not a public, so I can't drag and drop button because it's a private. So what we can do for achieving this ?
We have several ways... :)
First... We can use a public function from canvas class namely GetComponentsInChildren<Button>(), this function will gonna return all of the button within this canvas as an array. Here is the example code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponentsInChildren<Button> ();
		Debug.Log (buttons[0].tag);
		Debug.Log (buttons[1].tag);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

When we run the game, we'll see that button[0] contains button for restart and another for exit
Another function that we can use is GetComponents. Here is the example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Button button;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponents<Button>();
		Debug.Log (buttons);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
but unfortunately, for unity 5.6, GetComponents doesn't work... :v
So we can just use GetComponentsInChildrend ... :)

Video :

Friday, May 26, 2017

Roll Ball Unity 5.6 Part 5

Part IV bisa dilhat disini....
Okie... di part 5 ini pertama-tama kita tambain tag ke prefabnya biar tar pas ketabrak sama bolanya bisa dideteksi... So select prefabnya, terus disudut kanan atas add new tag kek gini :




Naaah... klu prefabnya diset Tagnya ke hisoka_target, makaaaaa...semua target bakalan punya tag hisoka_target juga...
That's the power of prefabs... :D
Siiipsss... abis nambain tag, berikutnya kita tambain rigidbody ke prefabsnya biar sesudah di hit sama bolanya, cachenya bisa ilang atau g' direcalculate ulang meshnya sama unity, ini buat penghematan memory... :)
Abs ngeadd rigidbody, ganti propertinya jadi kek gini :
Ooowh..terus centang 'Is Trigger' di properti box collidernya biar tar bisa masuk ke method OnTriggerEnter :
Okiee... Next kita import partikel package....
Terus setelah di import, masuk ke folder Prefabs ParticelSystems
Naaah... disitu kita bisa milih2 banyak object prefabs.... daaaan... disini Hisoka milih yang Flare sm Fireworks ajjah... :)
So buat folder baru, namanya "Resources" (ini namanya harus sama dan pake Kapital R)
Naaah... drag and drop fireworks & flare ke folder Resources ini ....
Siiipsss.... naaah.... waktunya main script lagi... :D
Silahkan buka script buat ShereController yang udah dibuat dari part2 sebelumnya, terus tambain method OnTriggerEnter kyk gini :

Method onTriggerEnter itu ke trigger pas bolanya ngehit kubusnya. Terus pas bola ngehit cube, dicek apa tagnya sama dengan hisoka_target, klu sama tar kode dari line 30 smapai 37 dieksekusi yng intinya ngejalanin Flare terus ngedestroy(7 detik setelah hit, kode line 35) dan cube dihide pake method SetActive(false);
Demonya kyk gini... :
Seeeepssss... sekian dl untuk part V... :)


Part 6 bisa dilihat di link ini....