• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by mtdrume · Jan 28, 2015 at 06:37 PM · c#androiderrortouch

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;
                      }
              }
  
          }
  }



Comment
eshedye

People who like this

1 Show 0
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
Best Answer

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.

Comment
mtdrume

People who like this

1 Show 7 · 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 mtdrume · Jan 28, 2015 at 07:59 PM 0
Share

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 : MonoBehaviour
 {
     
     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);
             }
         }
     }
 }

avatar image zharik86 · Jan 29, 2015 at 07:14 AM 0
Share

@Drumedia Are you create and add prefab (for line "public Rigidbody projectile;") in your script in Inspector?

avatar image mtdrume · Jan 29, 2015 at 07:18 AM 0
Share

I have added a sphere to my scene, and dragged it into Inspector and given it the RigidBody componant. Is that right?

avatar image zharik86 · Jan 29, 2015 at 08:24 AM 0
Share

Right, but if you sphere as bullet, create it as prefab. Are you have in your scene component by name "Main Camera". I see, your error in line 14 - line, where created Ray.

avatar image mtdrume · Jan 29, 2015 at 09:13 AM 0
Share

Yeh my camera is Main Camera, and my sphere is now a prefab, but it still returns error :(

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

foreach (Touch touch in Input.touches) not working 1 Answer

Unity Editor & iOS - Download MP4 to Streaming Assets Folder 0 Answers

Character Movement Problem 0 Answers

Distribute terrain in zones 3 Answers

Unexplainable NullReferenceException 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