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 3 · Feb 18, 2013 at 12:01 AM · collisionguiiosraycastmobile

How to pick up object on touch?

So I'm trying to pick up a flashlight when the user is both touching the screen and the ray is hitting the flashlight. Obviously it doesn't work, but there arent any errors that prevent the game from playing, all it says is: NullReferenceException: Object reference not set to an instance of an object Ray.Update () (at Assets/Scripts/Ray.js:23)

I used to have gameobject.tag, and they matched up, but it still said that, sorry for the poor explanation. Any help would be greatly appricated, I'm new to IOS programming in Unity, but pretty experienced with Unity in all. Thanks, here is the script I used:


 #pragma strict
 
 function Start () {
 
 }
 var guiray : GUIText;
 var flashlight : GameObject;
 var flashlightdesk : GameObject;
 function Update () {
       var hit : RaycastHit;
 var ray = camera.ScreenPointToRay (Vector3(200,200,0));
      Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
        var fwd = transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast (transform.position, fwd, 10)) {
      //   print ("There is something in front of the object!");
   //  var fingerCount = 0;
  //   for (var touch : Touch in Input.touches) {
  //       if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    //         fingerCount++;
  //   }
   //      if (fingerCount > 0){
   //      print ("User has " + fingerCount + " finger(s) touching the screen");
 if(hit.collider.gameObject.name == flashlightdesk) {
 guiray.text = "Pick Up Flashlight"; 
     if (Input.touchCount > 0) {
    //place pick up code here!
    flashlight.gameObject.active = true;
       //flashlight.gameObject.enabled = true;
    flashlightdesk.gameObject.active = false;
 }
 }
 else {
 guiray.text = "";
 }
 }
 }
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
2
Best Answer

Answer by RyanZimmerman87 · Feb 18, 2013 at 12:06 AM

On first glance it looks like you did not use your Raycast "Hit" variable in your raycast. Try something like this:

if (Physics.Raycast(ray, out hit, 100)

I don't know if the out is required in java this is C# example.

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 3 · Feb 18, 2013 at 12:17 AM 0
Share

Its weird how something that simple can cause hours of frustration, thanks a ton.

Also, do you know how I could make it, so that if my hands are already on the screen, it wouldn't pick it up? I would have to maybe hold my finger down?

avatar image RyanZimmerman87 · Feb 18, 2013 at 02:16 AM 0
Share

You can shoot a single ray on finger tap with:

if (Input.GetButtonDown ("Fire1"))

I would then first create an object that you can use to detect what your ray hit:

GameObject rayHitObject;

after casting your ray do this:

rayHitObject = hit.collider.gameObject;

Then create a script and attach it to your flashlight, even if it's empty it will let you identify your object. For example:

FlashLightScript

once you do that after you cast your ray you can use your rayHitObject to see if it hit your FlashLightScript. Like this:

if (rayHitObject.GetComponent()) {

in between put FlashLightScript I don't know why it won't let me post that code on forum.

Here is the official documentation for how to do it because my code is not posting correctly:

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

(then your desired behavior)

}

Another useful thing you may want to do is create a Vector point from your hit. Which looks like this:

rayVector = hit.point;

You can use that for functions like:

transform.LookAt(rayVector);

Or use it in a variety of useful ways.

If you are having trouble identifying the right object your ray may not be working correctly, or you may need to ignore collisions for certain unwanted objects.

avatar image RyanZimmerman87 · Feb 18, 2013 at 02:27 AM 0
Share

If you want to hold down your finger use:

if (Input.GetButton ("Fire1"))

ins$$anonymous$$d of:

if (Input.GetButtonDown ("Fire1"))

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make the size of GUI objects the same across all resolutions? 3 Answers

Button as a child of a button 0 Answers

Using Unity for a IOS Menu Only 0 Answers

Why isn't hit.transform.name with ray working? 1 Answer

size of GUI pics too big 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