• 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 Tyler Starr · Jan 29, 2011 at 08:26 PM · nullreferenceexception

Raycast Tap Help

I have this Script:

private var hit : RaycastHit; private var ray : Ray;//ray we create when we touch the screen

function FixedUpdate () { if(iPhoneInput.touchCount == 1) { ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position); Debug.DrawLine(ray.origin,ray.direction 10); if(Physics.Raycast(ray.origin, ray.direction 10,hit)){ if(!gameObject.rigidbody){ gameObject.AddComponent(Rigidbody); } } }
}

And I get this error: NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) Touchcontrol.FixedUpdate () (at Assets/Standard Assets (Mobile)/Scripts/Touchcontrol.js:8)

This seems to work in unity iphone 1.5 ,but not in unity3.

How can it be fixed?

Or how about another script to tap a collider on iphone)

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by karl_ · Jan 30, 2011 at 12:02 AM

Well, I believe iPhoneInput was nixed in favor of Input.touches with Unity 3. It seems like you could probably just replace the iPhoneInput.touches[0] with Input.touches.

I'm not near a machine running Unity right now, but I do happen to have a debugging script I wrote a while back when I was learning touch detection.

This may or may not be useful:

var locX : float = 5.5; var locY : float = .2; var sizeX : int = 500; var sizeY : int = 40;

private var beginLocation : Vector2; private var endLocation : Vector2;

private var isMoving : boolean; private var isColliding : boolean = false;

// Move cube vars private var velocity : Vector2; var smoothTime : float = .15; private var newCubePos : Vector2;

// Raycast vars var tagToMove : String = "moveable"; private var hit : RaycastHit; private var debugRay : Ray;

function Update() {
for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { isMoving = true; beginLocation = touch.position;

         // Check for gameobject contact
         var boom = Camera.main.ScreenPointToRay (touch.position);
         debugRay = boom;
         if (Physics.Raycast(boom, hit)) 
         {
             if(hit.collider.tag == tagToMove)
             isColliding = true;
         }
         else
         if(hit.collider.tag != tagToMove)
         {
             isColliding = false;
         }
     }   
     if (touch.phase == TouchPhase.Moved && isColliding) 
     {
         newCubePos = Camera.main.ScreenToWorldPoint(touch.position);
         this.transform.Translate(newCubePos.x-this.transform.position.x, newCubePos.y-this.transform.position.y, 0);            
     }
     if (touch.phase == TouchPhase.Ended) 
     {
         endLocation = touch.position;
         isColliding = false;
         isMoving = false;
     }   
 }

}

function OnGUI() { GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)), sizeX, sizeY), "Touch begin " + beginLocation); GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+20), sizeX, sizeY), "Touch end " + endLocation); GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+40), sizeX, sizeY), "Hit Object " + isColliding); GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+60), sizeX, sizeY), "Touching Screen " + isMoving); GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+80), sizeX, sizeY), "Obj Pos " + this.transform.position + " Ray Pos " + debugRay); }

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

NullReferenceException while trying to access script on object 2 Answers

instantiate prefab object to existing object 1 Answer

NullReferenceException in a GUI Dialog Method 1 Answer

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException 1 Answer


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