- Home /
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 :)
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.
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.
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 ?
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.
Try with Player_$$anonymous$$ale(Clone), Clone word with Capital C, maybe the problem is only because case-sensitiveness.
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 :)