Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by j-delhoyo · Oct 26, 2016 at 04:39 PM · raycasttargetting

problem with targeting system with raycast;

HI I'm new to programming and I'm trying to join a couple of scripts. the raycast part of the script seems to return an nullreferenceexception and I don't find the solution. The code is :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class TargettingSystem : MonoBehaviour {
 
 
     public List <Transform> targets;
     public Transform selectedUnit;
     void Start()
     {
         targets = new List <Transform> ();
         AddAllEnemies ();
         selectedUnit = null;
     }
     void Update () 
     {
         if (Input.GetKeyDown (KeyCode.Tab)) 
         {
             targetEnemy ();
         }
         if (Input.GetMouseButtonDown(0))  
         {
             SelectTarget ();
         }
     }
     void SelectTarget()
     {
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, 10000)) 
         {
             if (hit.transform.tag == "Enemy") 
             {
                 selectedUnit = hit.transform.gameObject.transform;
             }
         }
     }
     public void AddAllEnemies()
     {
         GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
         foreach (GameObject enemy in go)
             AddTarget (enemy.transform);
 
     }
     public void AddTarget (Transform enemy)
     {
         targets.Add(enemy);
     }
     private void targetEnemy()
     {
         selectedUnit = targets [0];
     }

}

Comment
Add comment · Show 3
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 oStaiko · Oct 26, 2016 at 06:13 PM 0
Share

What line was the console error on?

avatar image j-delhoyo oStaiko · Oct 26, 2016 at 07:19 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object TargettingSystem.SelectTarget () (at Assets/Scripts/PlayerCombat/TargettingSystem.cs:29) TargettingSystem.Update () (at Assets/Scripts/PlayerCombat/TargettingSystem.cs:23)

avatar image metalted · Oct 26, 2016 at 07:53 PM 0
Share

By the way, your SelectTarget() is specific to assign 1 variable, the "selectedUnit" variable. You could also use a return in the function and make something like this:

 void Update () 
 {
 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Tab)) 
 {
 TargetEnemy()
 }
 if (Input.Get$$anonymous$$ouseButtonDown(0))  
 {
 selectedUnit = SelectTarget ();
 }
 }
 
 private Transform SelectTarget()
 {
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 RaycastHit hit;
 
 if (Physics.Raycast (ray, out hit, 10000)) 
 {
 if (hit.transform.tag == "Enemy") 
 {
 return hit.transform.gameObject.transform;
 }
 return null;
 }
 return null;
 }

Its not a must, but there's useful stuff like break, continue, return etc. you can use in your methods.

1 Reply

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

Answer by metalted · Oct 26, 2016 at 07:45 PM

Because the error takes place in line 29:

 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

It could be a ray issue, a camera issue or an input issue.

Ray ray (should be a ray) > ScreenPointToRay() (That's a ray).

Camera.main > Should be a camera. Are you sure about this? Maybe try to assign the camera to a variable to see if there are any problems there. I saw something in this post:

http://answers.unity3d.com/questions/124998/cameramain-not-working-in-c.html

Maybe you have a script called "Camera" that is messing things up?

Input.mousePosition, cant see anything wrong there. The combination of ScreenPointToRay(Input.mousePosition) is used a lot so I don't see why there would be a problem there.

Try to mess around with the camera, maybe that's where the problem is. Because when your "Camera.main" isn't a real camera, you cant call a method like ScreenPointToRay on it, and it will return a error.

Comment
Add comment · Show 1 · 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 j-delhoyo · Oct 26, 2016 at 09:13 PM 0
Share

thanks, yes it was a problem with the camera, already fixed

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Targetting two objects at once 2 Answers

Mouse aim in orthographic view 0 Answers

raycast rigidbody addforce 2 Answers

Problem With Raycasthit Angle 0 Answers

How can I make a grappling hook with RayCast? 1 Answer


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