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 /
This question was closed Dec 20, 2016 at 02:27 PM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SidusLevis · Jul 16, 2013 at 12:20 PM · variable

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;
     
 }
 
Comment
Comments Locked · Show 4
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 amphoterik · Jul 16, 2013 at 12:38 PM 0
Share

Are both scripts on the same object? If not, then GetComponent won't do what you need.

avatar image SidusLevis · Jul 16, 2013 at 12:47 PM 0
Share

They aren't...

avatar image amphoterik · Jul 16, 2013 at 12:48 PM 0
Share

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?

avatar image SidusLevis · Jul 16, 2013 at 12:50 PM 0
Share

Health script is with my player object while the Attack script is with the AI object.

1 Reply

  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 4 · 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 SidusLevis · Jul 16, 2013 at 12:54 PM 0
Share

Thank you very much! :D It's working perfectly!

avatar image amphoterik · Jul 16, 2013 at 12:55 PM 0
Share

Glad to hear it.

avatar image Bubinga_Studios · Dec 20, 2016 at 02:23 PM 0
Share

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.

@amphoterik

avatar image Landern Bubinga_Studios · Dec 20, 2016 at 02:27 PM 0
Share

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

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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