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 Lewis_Game · Apr 28, 2016 at 05:13 AM · c#getcomponentvariables

How can I access variables from other scripts? c#

I have seen many other post about this but i just cannot seem to get it to work. I'm trying to use unity's HandHeldCam script as my main camera. I have made the variables that i want to change in the HandHeldCam script to public, what i want to do is make it so that when the player has under a certain health the camera starts to sway slightly more. I am able to do all the rest myself but i just cam seem to get the m_SwaySpeed over to my other script

 namespace UnityStandardAssets.Cameras
 {
     public class HandHeldCam : LookatTarget
     {
         public float m_SwaySpeed = .5f;
         public float m_BaseSwayAmount = .5f;

I need to access these variables from this script

 public Camera Cam;
 public float playerHealth;
 
     void Start () {
     playerHealth = 100;
     Cam = GetComponent<Camera>();
     }

Using things like public HandHeldCam Sway; don't seem to work If anyone can help that would be amazing.

Comment
Add comment · 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 JBehreandt · Apr 28, 2016 at 09:47 AM 1
Share

Hi Lewis_Games, I went in and modified the HandHeldCam script to give myself public getters and setters for its private variables.

 using System;
 using UnityEngine;
 
 namespace UnityStandardAssets.Cameras
 {
     public class HandHeldCam : LookatTarget
     {
         [SerializeField] private float m_SwaySpeed = .5f;
         [SerializeField] private float m_BaseSwayAmount = .5f;
         [SerializeField] private float m_TrackingSwayAmount = .5f;
         [Range(-1, 1)] [SerializeField] private float m_TrackingBias = 0;
 
         // ...
 
         #region getters
         public float getSwaySpeed() {
             return this.m_SwaySpeed;
         }
 
         public float getBaseSwayAmount() {
             return this.m_BaseSwayAmount;
         }
 
         public float getTrackingSwayAmount() {
             return this.m_TrackingSwayAmount;
         }
 
         public float getTrackingBias() {
             return this.m_TrackingBias;
         }
         #endregion
 
         #region setters
         public void setSwaySpeed(float value) {
             this.m_SwaySpeed = value;
         }
             
         public void setBaseSwayAmount(float value) {
             this.m_BaseSwayAmount = value;
         }
 
         public void setTrackingSwayAmount(float value) {
             this.m_TrackingSwayAmount = value;
         }
 
         public void setTrackingBias(float value) {
             if (value < -1f) {
                 value = -1f;
             }
             if (value > 1f) {
                 value = 1f;
             }
             this.m_TrackingBias = value;
         }
         #endregion
     }
 }

I then included a using statement at the top of the script that needed to access it.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Cameras;
 
 // If this script is on the same GameObject as HandHeldCam
 // [RequireComponent(typeof(HandHeldCam))]
 public class HandHeldAccess : $$anonymous$$onoBehaviour {
     private HandHeldCam hhc;
 
     void Awake() {
         // If this script is on the same GameObject as HandHeldCam
         // hhc = GetComponent<HandHeldCam> ();
         hhc = Camera.main.GetComponent<HandHeldCam>();
     }
 
     void Start() {
         Debug.Log("base sway before: " + hhc.getBaseSwayAmount());
         Debug.Log ("sway speed before: " + hhc.getSwaySpeed());
         Debug.Log ("tracking bias before: " + hhc.getTrackingBias());
         Debug.Log ("tracking sway amount before: " + hhc.getTrackingSwayAmount());
 
         hhc.setBaseSwayAmount (1.5f);
         hhc.setSwaySpeed (2.75f);
         hhc.setTrackingBias (3.825f);
         hhc.setTrackingSwayAmount (5);
 
         Debug.Log("base sway after: " + hhc.getBaseSwayAmount());
         Debug.Log ("sway speed after: " + hhc.getSwaySpeed());
         Debug.Log ("tracking bias after: " + hhc.getTrackingBias());
         Debug.Log ("tracking sway amount after: " + hhc.getTrackingSwayAmount());
     }
 }


alt text

outcome.png (71.3 kB)
avatar image Lewis_Game JBehreandt · Apr 28, 2016 at 04:04 PM 0
Share

Doing that gets me this error NullReferenceException: Object reference not set to an instance of an object HandHeldAccess.Awake () (at Assets/HandHeldAccess.cs:9) Not sure if it has to do with the camera being a child of the object with the health script but i cant seem to get it to work. For some reason i remember an easier way of doing this.

Thanks for the help

avatar image Lewis_Game Lewis_Game · Apr 28, 2016 at 04:09 PM 0
Share

Never $$anonymous$$d thanks for the help i managed to fix it. Forgot to set the camera as the main cam.

Still feel like there is an easier way to do this buy using getcomponentinchildren or something but i can't remember.

Thanks for the help Lewis

Show more comments

2 Replies

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

Answer by Lewis_Game · Apr 30, 2016 at 01:35 AM

I was able to find the best way to get the script from the other gameobject. Something that i realized that I completely forgot was to get the game object, public gameobject other; This made it so that the script could be found. This was my mistake, but thanks for the help anyway.

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 dandelo99 · Apr 28, 2016 at 09:08 AM

try this Cam.GetComponent(Camera).m_SwaySpeed; instead of this line Cam = GetComponent();

You can change variable with this for example;

  if (something) 
 {
 Cam.GetComponent<Camera>().m_SwaySpeed = 1f;
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Left hand side of an assignment must be a variable, a property or an indexer 1 Answer

c# how to use a variable from another script 1 Answer

Dynamically create variable name from a loop 1 Answer

GetComponent null, but it's attached to gameobject 1 Answer

How to access a variable from another C# script in Unity 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