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
0
Question by m5k · Sep 22, 2012 at 11:49 PM · c#guiclicktargetnpc

Passing values between scripts

Hello, i am currently working on setting up a targeting system, however i am facing an issue with passing a onmouse action to the player character.

Here is a snippet from the NPC which is to be targeted:

 // Npc Variables
         public string NPC_Name;
         Transform Location;
         
     
         // Used for initialization
         void Start () 
         {
             Location = gameObject.transform;
         }
         
         // Used for entity selection
         void OnMouseUp () 
         {
             GameObject.Find("Player_Male(clone)").GetComponent<Targeting>().Target_Name = NPC_Name;
             GameObject.Find("Player_Male(clone)").GetComponent<Targeting>().Target = Location;
         }
 

Here is the code it is to be manipulated by:

 public Transform Target;
         public string Target_Name;
     
         void Start () {
             Target = null;
         }
     
         
         void OnGUI () {
             if (Target != null){
             GUI.Box (new Rect (Screen.width/2,10,80,50), Target_Name);
             }
         }
 

however upon compilation i recieve the following error when clicking my NPC:

 NullReferenceException
     UnityEngine.GameObject.GetComponent[Targeting] () (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:18)
     NPC.OnMouseUp () (at Assets/Scripts/NPC.cs:29)
     UnityEngine.SendMouseEvents:DoSendMouseEvents()

Line 29 of NPC.cs is "GameObject.Find("Player_Male(clone)").GetComponent().Target_Name = NPC_Name;".

Thanks for any help you can offer :)

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by eatsleepindie · Sep 23, 2012 at 12:22 AM

Make sure the Targeting script is in fact attached and enabled on your Player_Male game object. Also, unless I'm mistaken, it's much more efficient to find game objects by tags rather than by name... so add a "Player_Object" or any other unique tag to your game object and find it with :

 GameObject.FindGameObjectWithTag('tag');

or by looping through

 GameObject.FindGameObjectsWithTag('tag');

Hope this helps, I'm eager to give back to this awesome community that has helped me so much in the last year.

Comment
Add comment · 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
0

Answer by m5k · Sep 23, 2012 at 10:57 AM

Thanks for you reply, i can confirm the script is attached to the Plater_Male gameobject and is indeed enabled.

To further expand on the issue, my game is a tiny scale mmo/multiplayer game, currently each player is a instance of Player_Male, with the local controllerable entity being Player_Male(clone) and networked players being Player_Male(remote).

Currently both local and remote players employ the player tag, so for the time being i am not using the player tag method for targeting.

Comment
Add comment · 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
0

Answer by Giacomino · Sep 23, 2012 at 01:10 PM

To ease debug I suggest you to split GameObject lookup and GetComponent method in different part, should be also more performing avoid Multiple gameobject lookups:

 GameObject MalePlayer = GameObject.Find("Player_Male(clone)") as GameObject; 
 Targeting target = MalePlayer.GetComponent<Targeting>();
 target.Target_name = "blabla";
 target.Target = Location;

And also.. Are you sure the script is called "Targeting" ? Are you sure is component of Player_Male ? Do you really want pass a Trasform or only a position (Vector3) ? Have you considered SendMessage() between objects instead script/component lookups ?

Comment
Add comment · 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
0

Answer by m5k · Sep 23, 2012 at 02:53 PM

The 2 scripts are indeed Targeting.cs and NPC.cs, i have used your recommendations and the same issue still persists so i will need to look into any possible cause. The playable character currently is named Player_Male, which upon instancing is refereed to Player_Male(clone) for the local player.

I have set the system up to transform rather than position for further expansion to the system in the future.

Below i have attached the scripts in full in case i have made any silly mistakes that i myself may have overlooked.

 //NPC.cs
 using UnityEngine;
 using System.Collections;
 
 public class NPC : MonoBehaviour {
     
     // Name label camera correction variables
     Camera cam;
     Transform camTransform;
     
     // Npc Variables
     public string NPC_Name;
     public int Health;
     public bool IsMob = false;
     public bool IsNPC = false;
     Transform Location;
     
 
     // Used for initialization
     void Start () 
     {
         Location = gameObject.transform;
         cam = Camera.main;
         camTransform = cam.transform;
     }
     
     // Used for entity selection
     void OnMouseUp () 
     {
         GameObject MalePlayer = GameObject.Find("Player_Male(clone)") as GameObject;
         Targeting target = MalePlayer.GetComponent<Targeting>();
         target.Target_Name = NPC_Name;
         target.Target = Location;
         //GameObject.Find("Player_Male(clone)").GetComponent<Targeting>().Target_Name = NPC_Name;
         //GameObject.Find("Player_Male(clone)").GetComponent<Targeting>().Target = Location;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnGUI () {
         
         if (IsNPC == true){
         GUI.color = Color.yellow;
         }
         if (IsMob == true){
         GUI.color = Color.red;
         }
         
          Vector3 offset = new Vector3(0, 3, 0);
          
           Vector3 point = Camera.main.WorldToScreenPoint(transform.position + offset);
          point.y = Screen.height - point.y;
         
         if (Vector3.Dot(cam.transform.forward,this.transform.position - camTransform.position) <= 0 ){
             GUI.Label(new Rect(point.x - 20, point.y - 20, 200, 20), "");}
         
         else{
             GUI.Label(new Rect(point.x - 20, point.y - 20, 200, 20), NPC_Name);}
 }
 }
     

 //Targeting.cs
 using UnityEngine;
 using System.Collections;
 
 public class Targeting : MonoBehaviour {
     
     public Transform Target;
     public string Target_Name;
 
     void Start () {
         Target = null;
     }
 
     
     void OnGUI () {
         if (Target != null){
         GUI.Box (new Rect (Screen.width/2,10,80,50), Target_Name);
         }
     }
 }
 

As you can see, the script is also responsible for naming and displaying the NPC name about the target's head, it also houses correction to stop the NPC name being projected when the target is not on screen.

Once again thanks for any help you can offer with this issue...as i am currently stumped.

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 Giacomino · Sep 23, 2012 at 08:27 PM 0
Share

Try with Player_$$anonymous$$ale(Clone), Clone word with Capital C, maybe the problem is only because case-sensitiveness.

avatar image
0

Answer by m5k · Sep 23, 2012 at 09:14 PM

"Try with Player_Male(Clone), Clone word with Capital C, maybe the problem is only because case-sensitiveness." - Giacomino

Thanks, i just discovered that too lol... i am feeling like a noob now lol. thanks alot for your help, as you message was a comment it seems i am unable to vote/select it as the answer :(

Thanks for your time and advise, you have helped me a lot :)

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

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

11 People are following this question.

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

Related Questions

Cannot modify a value type return value of... 1 Answer

urgent, pls help. Selection through keyinput in Canvas 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I script a GUI Texture as a button, if it's being called from a public variable? [C#] 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