- Home /
The question is answered, right answer was accepted
Passing Variables to another script
I'm trying to pass a variable from my HealthScript to my AI attackscript but I get nullreferenceexception errors. I'm not sure of how to pass it, can anyone check my scripts?
AI AttackScript
using UnityEngine;
using System.Collections;
public class AttackScript : MonoBehaviour {
Transform player;
Transform _transform;
float sqrRange = 3;
AIScript AIscript;
public GUIStyle customGuiStyle;
//Temp HP
float HP = 100f;
//Accessing HP script
HealthScript HPscript;
void Start(){
HPscript = GetComponent<HealthScript>();
AIscript = GetComponent<AIScript>();
player = GameObject.FindGameObjectWithTag("Player").transform;
if (player == null)
Debug.LogError("No player on scene");
_transform = GetComponent<Transform>();
}
void AttackColliision(){
Vector3 direction = player.position - _transform.position;
if(AIscript.animation.IsPlaying("G_AttackScript") && (direction.sqrMagnitude < sqrRange)){
if(Vector3.Dot (direction.normalized,_transform.forward) > 0){
//Damage Script
HPscript.HP -= 50;
if(HPscript.HP <= 0){ Time.timeScale = 0; AudioListener.pause = true;
print(HPscript.HP);
}
}
}
}
void Update(){
Debug.Log(HPscript.HP);
if (HPscript.HP < HPscript.maxHP) HPscript.HP += HPscript.healSpeed * Time.deltaTime;
}
void OnGUI(){
GUI.Box (new Rect (Screen.width - 125,10,100,20), "Health: " + HPscript.HP, customGuiStyle);
if(HPscript.HP <= 0){
GUI.Box (new Rect (Screen.width / 2,Screen.height / 2,100,50), "You are Dead!", customGuiStyle);
}
}
}
My HealthScript using UnityEngine; using System.Collections;
public class HealthScript : MonoBehaviour {
public float HP = 100f;
public float maxHP = 100f;
public float healSpeed = 5f;
}
Are both scripts on the same object? If not, then GetComponent won't do what you need.
O$$anonymous$$, one second I will answer. Also, are either of the scripts on the object with script or are both of them somewhere else?
Health script is with my player object while the Attack script is with the AI object.
Answer by amphoterik · Jul 16, 2013 at 12:51 PM
You will need to change your variable declaration to be public like:
public HealthScript HPscript;
Then, in the editor, simply drag the object that has the script on it over to the variable propertie in the inspector view.
Now, if the scripts are on prefabs that are instantiated, you obviously won't be able to drag and drop them in the editor (they won't exist yet). Instead, you will need to find the objects and then get the components. This SHOULD happen in the start method (assuming they have been instantiated before that):
HPScript = GameObject.Find("Name of Object").GetComponent<HealthScript>();
Do the same for the AI script (if needed).
I tried this, but it doesn't seem to work...
public class ThirdPersonUserControl : $$anonymous$$onoBehaviour
{
private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
private Transform m_Cam; // A reference to the main camera in the scenes transform
private Vector3 m_CamForward; // The current forward direction of the camera
private Vector3 m_$$anonymous$$ove;
private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
public bool Aim;
private void Start()
This is my first script ^ (Part of it)
float m_CapsuleHeight;
Vector3 m_CapsuleCenter;
CapsuleCollider m_Capsule;
bool m_Crouching;
public GameObject AimPivot;
public ThirdPersonUserControl Aim;
Aim = GameObject.Find("Aim").GetComponent<ThirdPersonUserControl>(); //This Doesn't work :(
void Start()
{
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
This is my second ^ (Part of it)
I can't seem to send this boolean "Aim" to another script, and I can't seem to find out why...
I did what amphoterik said to do, but it doesn't work. Is it outdated? Please get back to me and tell me how to do it.
The first script shows nothing interesting or how you're trying to get the reference.
The second script shows you trying to set a field outside a method in a class which is incorrect syntax and will throw an error in monodevelop/visual studio. You will need to set Aim in a method(like say Start) but the reference(gameobject) should exist prior to trying to find it. Also the GameObject must be named Aim otherwise it will not find a GameObject reference to try and get a Component class named ThirdPersonUserControl. You should probably post another question ins$$anonymous$$d of a 3 year old one. The component reference question exists in the thousands if not tens of thousands on this site, do some research and go through the tutorials on unity3d.com. The component reference part of unity is essential to completing a project for the most part unless you set everything through the inspector.
Follow this Question
Related Questions
Communication between objects and other scripts, variables and properties 1 Answer
Rewind/Play animations on demand. 1 Answer
Can I show a numeric variable from the slider on the GUI? 1 Answer
variable inside prefab 1 Answer
How to: make changes to a var over real time units instead of frame units? Mathf.Lerp 1 Answer