Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by francy52 · Nov 20, 2015 at 02:05 PM · 2dvector3variablescript referencing

How do I link variables in between two scripts in c#

guys I feel silly have been looking the whole day in unity answers but my script still doesn't work been trying to use gameobject method to link my shieldHP with mytest manager to see if the shield would actually get destroyed and also sot htat I may do this in the future with other scripts Here they are First script: using UnityEngine; using System.Collections;

public class LightFolow : MonoBehaviour {

 public Object Shield;
 public Camera cam;
 private float MaxW;
 public Rigidbody2D Light;
 Vector3 targetPosistion; 
 public int LightHP;
 
 
 // Use this for initialization
 void Start () {
     if (cam == null) {
         cam = Camera.main;
     }
     Light = GetComponent<Rigidbody2D> ();
     Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
     Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
     MaxW = targetWidth.x;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
     Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
     float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
     targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
     Light.MovePosition (targetPosition);

     if (LightHP == 0 || LightHP < 0)
         Destroy (Shield);
     
 
 }

} and then : using UnityEngine; using System.Collections;

public class TestManager : MonoBehaviour {

 GameObject Shield;
  

 // Use this for initialization
 void Start () {
     LightFolow ShieldHealth = Shield.GetComponent<LightFolow> ();
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode("K"))
         ShieldHealth.LightHP - 20;
 }

} + I would also like to do this for my shield and player since they bother have the same target position obviously so if someone could help me carry that value over would be great Player script: using UnityEngine; using System.Collections;

public class PlayerChontrol : MonoBehaviour {

 public Camera cam;
 private float MaxW;
 public Rigidbody2D Player;
 Vector3 targetPosistion; 
 
 
 // Use this for initialization
 void Start () {
     if (cam == null) {
         cam = Camera.main;
     }
     Player = GetComponent<Rigidbody2D> ();
     Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
     Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
     MaxW = targetWidth.x;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
     Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
     float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
     targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
     Player.MovePosition (targetPosition);
     
     
 }

} Soz if the is bad I is nub and this is just a hobby but I would really like to learn more so pls help me :D

Comment
Add comment · Show 1
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 meat5000 ♦ · Nov 20, 2015 at 02:09 PM 0
Share

As long as Shield contains a valid GameObject and the script exists on that object, the GetComponent should work.

This is very common question. You should refer to existing answers as the information is the same throughout.

Note that if you declare a variable (LightFolow ShieldHealth) in Start() it will only exists within Start and cannot be used outside. Declare it in the class as you have done in the other scripts.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mako-Infused · Nov 20, 2015 at 02:23 PM

I fail to see the significance of the "PlayerChontrol" class, is there a particular reason you posted it? In any case, I think your problem lies within the "TestManager" class.

You're declaring the "ShieldHealth" as a local variable inside of the Start method. Then you attempt to use it inside Update. This isn't going to work, if you want to have access to the variable throughout the class, you'll need to declare it as a private variable inside of the class itself. Furthermore, if you intended to have the "LightHP" change, you're going need to use a -= operator.

Updated

This might be more appropriate:

 using UnityEngine; 
 using System.Collections;
 
 public class TestManager : MonoBehaviour
 {
      GameObject Shield;
      private LightFolow ShieldHealth;
 
     // Use this for initialization
      void Start () {
          ShieldHealth = Shield.GetComponent<LightFolow> ();
      }
      
      // Update is called once per frame
      void Update () {
          if (Input.GetKeyDown(KeyCode("K"))
              ShieldHealth.LightHP -= 20;
      }
 }
Comment
Add comment · Show 2 · 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 francy52 · Nov 21, 2015 at 02:28 AM 0
Share

Ty dude told you I'm a nub xD But it give me a parser error on ShieldHealth says it an unexpected symbol??

avatar image Mako-Infused francy52 · Nov 21, 2015 at 02:14 PM 0
Share

Oh, did you just copy/paste and replace what you originally had in the Test$$anonymous$$anager class? I didn't rewrite the script fully, you still have to include some of the stuff you had in there originally. I'll edit it my post to make the answer more complete.

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

Vector3 variable not changing 0 Answers

Health variable not changing 1 Answer

Problem with Vector2/Vector3.Angle? 0 Answers

Problems with vector3 in world / local space 1 Answer

Assign GameObject OnMouseUp not through inspector -2D 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