- Home /
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 = "";
}
}
}
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.
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?
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.
If you want to hold down your finger use:
if (Input.GetButton ("Fire1"))
ins$$anonymous$$d of:
if (Input.GetButtonDown ("Fire1"))
Your answer
Follow this Question
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