• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

remove forward component from velocity vector 2 Answers

Raycast should ignore all childs of its parent objects of 1 Answer

SphereCastAll doesn't works 1 Answer

RAYCAST Forward error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges