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.
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());
}
}
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
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
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.
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;
}
Your answer
Follow this Question
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