- Home /
Move a cube from GUITexture. Is not working. Where is my mistake
Hello
I really hope someone can help me in this since I am working already two days but without any result.
What I' am trying to achieve in this instance is to move a GameObject when a GUI Texture is touch on a Iphone. The GameObject to be moved is named Cube in the hierarchy The Game Object Cube has a Script named Left that supposedly when "call it " from the GUITexture made the Cube move left.
I hope is clear I want to "activated" the script in the Game Object from the Guitexture. I try to use send message but without any joy as well so I am using GetComponent
This is the script "inside" the left GUITexture.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour { public GameObject Cube;//GameObject to be moved private Left left;//script inside the gameobject cube so it can move left when call it from the GUItexture
void Awake() { left = Cube.GetComponent (); }
void Start() { Cube = GameObject.Find ("Cube"); }
void Update () {
//is there a touch on screen
if (Input.touches.Length <= 0)
{
//if there is no touches on the screen the this code
return;
}
else // if there is a touch
{
//loop through all the touches on the screeen
for(int i = 0 ; i < Input.touchCount; i++)
{
//execute this code for current touch (i) on the screeen
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//if current hits our guiTecture, run this code
if(Input.GetTouch (i).phase == TouchPhase.Began)
//move the cube
Cube.GetComponent<Left> ();
}
{
if(Input.GetTouch (i).phase == TouchPhase.Ended)
Return;
{
if(Input.GetTouch(i).phase == TouchPhase.Stationary);
//if current finger is stationary run this code
Cube.GetComponent<Left> ();
}
}
}
}
}
This is the Script named Left that is "inside" the GameObject
public class Left : MonoBehaviour {
// Use this for initialization void Start () {
}
// Update is called once per frame void OnMousedown () { transform.position += Vector3.left * Time.deltaTime;
} }
I really HOPE someone can help me
Thank you
CL
Okay, so your Left
$$anonymous$$onoBehaviour is deactivated by default? In that case, change Cube.GetComponent ();
to Cube.GetComponent().enabled = true;
. That should do the trick.
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
more pragma strict getComponent and SendMessage problems 0 Answers
GetComponent for all enemies 1 Answer
pickup weapon with GetComponent 0 Answers
I am trying to send a 2D texture to my first person controller to display on the GUI 1 Answer