Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ramsey111 · Aug 15, 2015 at 03:37 AM · raycastingscriptingbasicstroubleshooting

New to Raycasts, trouble using "hit"

So I've been trying to get the hang of Raycasts for about a week now (slow learner) and I've finally gotten to the point where I can make my mouse tell me whether or not it's on top of something but now I'm trying to make it minus 1 from a variable containing the "enemy's" (cube's) life. The cube has 2 life and every time I click him it should subtract one and then once his life is zero it should print: "cube is dead" but I must be writing something wrong with the "hit" part because if I comment that part out the part that tells me the thing about where the mouse is works fine. Also just a warning in advance I'm away from home and so all I have to learn with is a crappy little netbook and the box with the code formatting literally won't load... I'm so sorry and as soon as I get back (in two days, I'd also do it now but I don't know BB code which I at least think is what these forums use) I'll make sure to format it correctly on my usual computer but for now here's the code (I'm really sorry if it's a bitch to read...) EDIT: I reloaded the page a couple of times and the buttons appeared so I think the code should be fine now, here it is:

 #pragma strict
 var enemyLife : float = 2;
 
 function Update () {
     if(Input.GetMouseButton(0)){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast (ray, hit)){
             print("hit");
         }
         else{
             print("did not hit");
         }
     }
     if(hit.transform.tag == "enemy"){
         enemyLife = enemyLife--;
         print("tag is not the problem");
     }
     if (enemyLife == 0){ 
          print("cube is dead");
     }
 
 
 }


The part where it says:

 if(hit.transform.tag == "enemy"){
         enemyLife = enemyLife--;
         print("tag is not the problem");
     }
 

originally said:

 if(hit.collider.tag == "enemy"){
         enemyLife = enemyLife--;
         print("tag is not the problem");
     }

I watched a tutorial and decided to change it.

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 tanoshimi · Aug 15, 2015 at 06:35 AM 0
Share

"I must be writing something wrong with the "hit" part because if I comment that part out the part that tells me the thing about where the mouse is works fine"... you've failed to describe what happens if you don't comment out "the hit part", and what part is that anyway? Commenting out line 6, for example, should give you an error.

avatar image Ramsey111 · Aug 15, 2015 at 07:18 PM 0
Share

Sorry, I think the part I was referring to was lines 15-21 or at least the problem arose when I wrote that part. and what happens if I leave it? This : NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript.Update () (at Assets/NewBehaviourScript.js:15)

I should have originally included that, sorry. It's the error message I get for doing something wrong.

Also I haven't ever even heard of On$$anonymous$$ouseUp I'll look it up in the scripting reference.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Question about movement forward in line of sight 0 Answers

Question regarding Raycast hitting UI button object instead of gameobject 0 Answers

Does Raycasting require a PhysX enabled video card? 1 Answer

identifying the index of a gameobject in an array 1 Answer

Leaderboard thing ? 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