Log in or register to post comments

Adding changing color objects to VirtualButtons...

September 5, 2012 - 10:14am #1

Hi !!

I'm trying to figure out how VirtualButtons works.

I'm new in Unity3d and i don't have yet a good comprension of the code and the usage of operators...

I've duplicated the teaspot and named teaspot2,  now i wanna try to give the changing color beaviour to both my objects...

I found this line of code

   // Get handle to the teapot object
        mTeapot = transform.FindChild("teapot").gameObject;

and without having a Knowledge base of c++ i've tried a lot of combinations  and maybe the best i came at is this

   // Get handle to the teapot object
        mTeapot = transform.FindChild("teapot")&&("teapot2").gameObject;

but obviosly is not working...Someone can help me ??

An other thought came in my mind...

If i wanna have in my scene 10 teapots and gave all of them the same changing color beaviuour i've to add them on by one one in the script ?? There is a way to tag them and use something like transorm.tag == "Mytag"??

Thanks in advance,

 

Adding changing color objects to VirtualButtons...

April 29, 2016 - 5:03pm #3

hello, i have trying to manipulating object with this script, but 3d object is not changing.

i used 3dsmax object, unity and vuforia sdk

thanks.

using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class VirtualButtonEvent : MonoBehaviour, IVirtualButtonEventHandler {
	
	private GameObject ChamferBox01;
	private GameObject ChamferBox02;
	private GameObject Cylinder01;
	
	// register buttons for event handling
	void Start() {
		VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
		for (int i = 0; i < vbs.Length; ++i) { vbs[i].RegisterEventHandler(this); }
		ChamferBox01 = transform.FindChild("ChamferBox01").gameObject;
		ChamferBox02 = transform.FindChild("ChamferBox02").gameObject;
		Cylinder01 = transform.FindChild("Cylinder01").gameObject;
	}
	
	// button is "pressed" so change color of Sphere
	public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
		if (vb.VirtualButtonName=="Red")  { ChamferBox01.GetComponent<Renderer>().material.color = Color.red;} 
			if (vb.VirtualButtonName=="Red")  { ChamferBox02.GetComponent<Renderer>().material.color = Color.red;}
			if (vb.VirtualButtonName=="Red")  { Cylinder01.GetComponent<Renderer>().material.color = Color.red;}
		if (vb.VirtualButtonName=="Blue")  { ChamferBox01.GetComponent<Renderer>().material.color = Color.blue;} 
		    if (vb.VirtualButtonName=="Blue")  { ChamferBox02.GetComponent<Renderer>().material.color = Color.blue;}
		    if (vb.VirtualButtonName=="Blue")  { Cylinder01.GetComponent<Renderer>().material.color = Color.blue;}
	}
	
	// change Sphere back to white
	public void OnButtonReleased(VirtualButtonAbstractBehaviour vb) {
		if (vb.VirtualButtonName=="Red")  { ChamferBox01.GetComponent<Renderer>().material.color = Color.black;} 
		if (vb.VirtualButtonName=="Red")  { ChamferBox02.GetComponent<Renderer>().material.color = Color.black;}
		if (vb.VirtualButtonName=="Red")  { Cylinder01.GetComponent<Renderer>().material.color = Color.black;}
		if (vb.VirtualButtonName=="Blue")  { ChamferBox01.GetComponent<Renderer>().material.color = Color.black;} 
		if (vb.VirtualButtonName=="Blue")  { ChamferBox02.GetComponent<Renderer>().material.color = Color.black;}
		if (vb.VirtualButtonName=="Blue")  { Cylinder01.GetComponent<Renderer>().material.color = Color.black;}
	}
	
}

 

Adding changing color objects to VirtualButtons...

September 5, 2012 - 11:12am #2

Yes you can obtain GameObjects by tag - see: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html & http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html

Also the tag reference - http://docs.unity3d.com/Documentation/Components/Tags.html & http://docs.unity3d.com/Documentation/ScriptReference/GameObject-tag.html

Here is an example using tag names..

     GameObject[] tpots = GameObject.FindGameObjectsWithTag("mytag");

   foreach (GameObject tpot in tpots) {
// do something foreach tpot
}

Also Unity uses C#, not C++ - perhaps that's what you meant.

 

Log in or register to post comments