- Home /
Merge 2 Script Touch
How can I merge this 2 script, I need a working touch button that play the animation touching it.
// Draws 2 buttons, one with an image, and other with a text // And print a message when they got clicked. var btnTexture : Texture; function OnGUI() {
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(Rect(10,10,50,50),btnTexture))
Debug.Log("Clicked the button with an image");
if (GUI.Button(Rect(10,70,50,30),"Click"))
Debug.Log("Clicked the button with text");
}
and this.
function Update () {
if(Input.touchCount >= 0) {
var touch : Touch = Input.touches[0];
if(touch.phase == TouchPhase.Began)
{
animation["Play"].speed= 1.0;
animation.Play("Play");
ResetAnimation(animation["Play"]);
}
}
}
function ResetAnimation(curAnim : AnimationState) { yield WaitForSeconds(curAnim.length); animation.Play("Idle"); }
Answer by Noah-1 · May 10, 2012 at 02:16 AM
This is a basic start, so you can get the idea:
var btnTexture: Texture2D;
function Update(){
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return; } }
function OnGUI(){
if (GUI.Button(Rect(10,70,50,30),"Click")){
Debug.Log("Clicked the button with text");
animation["YourAnimation"].speed= 1.0;
animation.Play("YourAnimation");
} }
Greetings
NullReferenceException: Object reference not set to an instance of an object
If you just copied my code, you were missing the update function so probably that was the issue. I already edited it, try it again.
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.Button (Rect position, System.String text) (at C:/BuildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/Editor/GUI.cs:221) b2.Update () (at Assets/b2.js:35)
Noah 1, it work great, but now once i touch the button the animation stay in looping, and not back to Idle.
Your answer

Follow this Question
Related Questions
(Mobile) Touch anywhere on screen EXCEPT button 3 Answers
Holding GUI Button Touch to Rotate Object 0 Answers
Restart Game button for android mobile Phone help? 1 Answer
Android 3D Touch for Menu 0 Answers
Button not responsive 1 Answer