Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by DeMetal · Mar 18, 2012 at 05:20 PM · c#targetingclickable

Problem with clicking to target enemies

So I'm looking to make an isometric game where the player clicks on enemies to target them, and the player will then move towards the enemy and begin attacking.

The player object has the following script attached

 using UnityEngine;
 using System.Collections;
 
 public class playerControllerScript : MonoBehaviour {
 
     public Transform Target;
     public Transform myTransform;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(!Target)
         {
             Debug.Log("No target");
         }
         else
         {
             myTransform = transform;
             Debug.DrawLine(transform.position, Target.position);
         }
     }
     
     public void changeTarget(Transform newTarget)
     {
         Target = newTarget;
     }
 }

so that the target is a position the player will move to, rather than an object (I thought this would be easier, all enemies will be spawned when the player gets close enough to the spawn point, and all enemies will have the same targetting script). This is it in the stages of sorting out targetting, attacking and moving will get done soon.

and the enemies have the following script attached

 using UnityEngine;
 using System.Collections;
 
 public class targettingScript : MonoBehaviour {
 
     public Transform myTransform;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         myTransform = transform;
     }
     
     void OnMouseDown()
     {
         playerControllerScript player = GetComponent<playerControllerScript>();
         player.changeTarget(myTransform);
         Debug.Log("Clicked on");
     }
 }

So when an enemy is clicked on, the player will have a new transform to move towards and attack. Again, this is still in the stages of sorting out targetting.

But when I click on a target, I get "NullReferenceException: Object reference not set to an instance of an object targettingScript.OnMouseDown ()" and nothing happens beyond that.

How do I fix this? Thanks in advance.

Comment
Add comment
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
1

Answer by davedx · Mar 18, 2012 at 07:37 PM

Your enemies are trying to access the playerControllerScript on themselves, which I guess they don't have? You probably just need to change how you're getting that script. The quick and dirty way would be to use GameObject.Find(playerGameObjectName).GetComponent. Better ways would be to use tagging to find your player object, or to set it with a public field on your enemies.

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 Dakwamine · Mar 19, 2012 at 02:00 AM 0
Share
  • for tagging —> FindWithTag

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can't target another player using mouseButtonUp 0 Answers

Problem with targeting system 0 Answers

multiple objects not behaving independently 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