- Home /
I have touch detected now how do I make it talk to my movement script ?
Ok so I have a 2.5D project I am messing around with. Now that I have the IOS/Android licenses I would like to make builds to test out on my phone. So I need to set up touch controls.
Here is a screenshot: ![alt text][1] [1]: http://desmond.imageshack.us/Himg17/scaled.php?server=17&filename=examplead.jpg&res=medium
Pretty simple set up that I need. I dont need anything to move/snap back I just need to know when the button is pressed.
Now I have code that lets me detect when the button is pressed and it registers on the unity remote fine. I have read through it numerous times so i understand it well. (This is attached to GUITexture)
var guiJumpButton : GUITexture;
var touchJump : boolean = false;
function Update()
{
if(Input.touchCount > 0)
{
for(var i : int = 0; i < Input.touchCount; i++)
{
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
Debug.Log("Can Jump");
touchJump = true;
}
else touchJump = false;
//Debug.Log ("No Jump");
}
}
}
Now what I thought I would do is be cool and when the button is pressed change a boolean to true, then I would access that boolean from my movement control script.(This is part of movement script attached to my character)
//Access to touch script
var accessToTouchControls = GetComponent(Script_Touch_Controls);
var accTouchJump = accessToTouchControls.touchJump;
//Allow Character to jump
if ( accTouchJump && ( controller.isGrounded ))
{
velocity.y = jumpHeight;
}
however when I do this I get a "Object reference not set to an instance of an object". So is my syntax bad or is this a completely wrong way to approach this ?
Any advice on how to properly get the buttons to talk correctly with my movement script would be much appreciated.
Answer by DaveA · Mar 23, 2012 at 08:20 PM
I'm guessing Script_Touch_Controls is not found? Make sure the Script_Touch_Controls script is on the same object, or you'll have to find the object that it's on.
hmmm.. I think you are correct. Because when I used GetComponent() before it was to access scripts that were on the same game object, these scripts are on two different objects. Ill have to research how to access a variable on another game objects script.
var otherObject; function Start() { otherObject = GameObject.Find("that other objects name"); }
... var accessToTouchControls =otherObject.GetComponent(Script_Touch_Controls).
Thanks man! that worked as well. I used the method of assigning it in the inspector. I read that using GameObject.Find can be expensive. $$anonymous$$uch appreciated
Answer by Bunny83 · Mar 24, 2012 at 02:41 AM
It's way better to setup your references at building time. Just offer a public variable of your other script type and assign the other object to the variable in the inspector via drag&drop:
var jumpcontrolScript : Script_Touch_Controls; // assign in the inspector
function Update()
{
if( jumpcontrolScript.touchJump && controller.isGrounded )
{
// Do something
}
}
btw. It's better to create more reusable code. You could use the Script_Touch_Controls script on multiple touch buttons. For this you should name your "output variable" something more common like .isPressed
.
The assignment which button controls what can be done via drag & drop in the inspector.
Thanks dude! Worked like a charm. I changed the output variable to isPressed as you suggested So I can use it on the other buttons. And I assigned the object that had the script on it I was trying to access in the inspector.
When you say change the "output variable" what exactly do you mean?
Your answer
![](https://koobas.hobune.stream/wayback/20220613065450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touch movement not working 1 Answer
TWO GUI texture that doesn't work together 0 Answers
Player not following touch after camera is rotated? 0 Answers
How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers
Help with iphone/android touch controls 2 Answers