Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by extol · Mar 23, 2012 at 08:11 PM · movementtouchguitexture

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image extol · Mar 23, 2012 at 08:55 PM 0
Share

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.

avatar image DaveA · Mar 24, 2012 at 01:44 AM 0
Share

var otherObject; function Start() { otherObject = GameObject.Find("that other objects name"); }

... var accessToTouchControls =otherObject.GetComponent(Script_Touch_Controls).

avatar image extol · Mar 27, 2012 at 05:15 PM 0
Share

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

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image extol · Mar 27, 2012 at 05:18 PM 0
Share

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.

avatar image kangta · May 04, 2012 at 05:55 PM 0
Share

When you say change the "output variable" what exactly do you mean?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges