• 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 /
  • Help Room /
avatar image
0
Question by Divinitize · Nov 13, 2015 at 02:59 AM · projectile

Can anyone help me get my projectile to work c#

Basically i had this working in Javascript, but im converting all my scripts to c#.

This is currently my main script so it's important it works correctly, i think i've made too many changes and i'm getting very lost. When i currently play and press my mouse button its giving me a null reference error.

Any help would be highly appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class ThrowKnife : MonoBehaviour 
 {
     public GameObject knife;
     public int power; 
     public bool heldDown= false;
     
     
     void  Start ()
     {
         knife = Resources.Load ("Knife_Prefab") as GameObject;
     }
     
     void  Update ()
     {   
         power = 20;
         
         if(Input.GetMouseButtonDown(0))
         {
             
             
             RaycastHit vHit = new RaycastHit();
             Ray vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(vRay, out vHit, 10000)) 
             {
                 Rigidbody clone;
                 clone = Instantiate(knife, transform.position+Camera.main.transform.forward * -2, Quaternion.identity)as Rigidbody; 
                 clone.transform.LookAt(vHit.point); 
                 clone.velocity = transform.TransformDirection(Vector3.forward * power);
                 power = 20;
 
             }
         }
     }
 }
 
Comment
Add comment · Show 2
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 pekalicious · Nov 13, 2015 at 03:12 AM 0
Share

Can you paste the null ref error? Typically they tell you which line the error is and makes things easier to figure out.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Nov 13, 2015 at 01:28 PM

I had issue before when trying to cast to Rogidbody with Instantiate.

 if(Physics.Raycast(vRay, out vHit, 10000)) 
 {
        GameObject clone = (GameObject)Instantiate(knife, transform.position+Camera.main.transform.forward * -2, Quaternion.identity; 
       clone.transform.LookAt(vHit.point); 
       clone.GetComponent<Rigidbody>().velocity = transform.TransformDirection(Vector3.forward * power);
       power = 20;
 }

Comment
Add comment · Show 6 · 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 Divinitize · Nov 13, 2015 at 01:34 PM 0
Share
 clone.GetComponent<Rigidbody>.velocity

This part, velocity is red in monodevelop and the error is expression denotes a method group, where a variable , value or type was expected.

Any help would be highly appreciated again.

avatar image Landern Divinitize · Nov 13, 2015 at 02:40 PM 1
Share

@fafase made a small typo, GetComponent is a method, change it to use parenthesis():

 clone.GetComponent<Rigidbody>().velocity

avatar image Divinitize Landern · Nov 13, 2015 at 04:23 PM 0
Share

This was the error thank you,

I do however have one more problem, using the same section of the script, its now firing my projectile wherever the camera is pointing, and i need it to fire where the mouse is pointing. It did this when it was javascript, but now ive made too many changes, it's no longer doing it...

Any further help with this matter would be highly appreciated. I will mark this as resolved even though i need further help with a different matter.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2 Issues with my SHUMP game 0 Answers

Linking projectiles to enemy 0 Answers

Script Projectile Project 0 Answers

Why are my instantiated projectiles not firing? 0 Answers

My enemy AI shoots projectile only to certain degrees. Apparently related to transform.position? 0 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