- Home /
Make sphere shoot to Touch.position error
So after an unapproved question, I'll ask this differently, as i don't understand, while i get various google results and tried multiple thing to fix this, my code still gets this error message.
error CS0120: An object reference is required to access non-static member `UnityEngine.Touch.position'
I have tried changing Touch.position to touch.position but this produces another error. I have tried putting in GetComponent in various places put i don't even know if it's what i ment to be doing.
If someone could help me out without just sending me to generic search results that would be amazing. My goal is just to have the projectile shoot toward to touch location.
This script is attached to the main camera, with a sphere as the projectile.
I'm sure this will serve well for others for a bread crumb to do the same.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour
{
public Rigidbody projectile;
void Update()
{
int i = 0;
if (Input.touchCount > i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Touch touch;
touch = Camera.main.ScreenPointToRay (Touch.position);
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(touch);
++i;
}
}
}
}
Answer by zharik86 · Jan 28, 2015 at 07:37 PM
Not use line "Touch touch;". Use Ray and see changing code below:
void Update() {
//Why do you create variable "i". Doing code without "i"
if (Input.touchCount > 0) {
if (Input.GetTouch(0).phase == TouchPhase.Began) {
//Use Ray for direction and right call touch event
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
//Right direction from ray
clone.velocity = transform.TransformDirection(ray.direction);
}
}
}
I hope that it will help you.
Thanks for your response. I changed my code to suggested, but i now receive the error:
NullReferenceException: Object reference not set to an instance of an object Shoot.Update () (at Assets/Scripts/Shoot.cs:14)
Code is now:
using UnityEngine;
using System.Collections;
public class Shoot : $$anonymous$$onoBehaviour
{
public Rigidbody projectile;
void Update() {
//Why do you create variable "i". Doing code without "i"
if (Input.touchCount > 0) {
if (Input.GetTouch(0).phase == TouchPhase.Began) {
//Use Ray for direction and right call touch event
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
//Right direction from ray
clone.velocity = transform.TransformDirection(ray.direction);
}
}
}
}
@Drumedia Are you create and add prefab (for line "public Rigidbody projectile;") in your script in Inspector?
I have added a sphere to my scene, and dragged it into Inspector and given it the RigidBody componant. Is that right?
Right, but if you sphere as bullet, create it as prefab. Are you have in your scene component by name "$$anonymous$$ain Camera". I see, your error in line 14 - line, where created Ray.
Yeh my camera is $$anonymous$$ain Camera, and my sphere is now a prefab, but it still returns error :(