I'm going crazy over here. All I want to do is reference a variable in one script from another. This variable can't be static though. I've gone through a million examples (most in Javascript, and I'm not so good at translating). Nothing is working. Here's an example of what I mean:
In a script called "PlayerScript" attached to "ThePlayer" game object:
public class PlayerScript: MonoBehaviour {
float Health = 100.0f;
}
What, very specifically, would I put into another script to get at that Health variable?
Notice that the scripting reference has C#, JS and Boo examples. You can toggle which language you want to get help for by selecting from the drop down on the right side near code snippets.
A. You need a reference to the game object that hold the script, or component that is placed on the same object of your script. This can be found with GameObject.Find, accessing transform children, (or just call GetComponent on the running script, see #B).
B. You use GetComponent on either the game object or component, to access your script.
@The OP: Note that you don't actually have to make 'Health' public. If you only want to read from it but not write to it, you can expose its value through an accessor or property. (Which you can still do if you want to write to it as well, of course.)
Yeah, the accessor/property idea is definitely predicated on the assumption that the field doesn't need to appear in the inspector. (If I have fields that don't need to appear in the inspector, I generally make them private and expose them via accessors if/as needed, but for anything that needs to appear in the inspector, I agree that just going with public is usually the most reasonable choice.)
THANK GOD FOR Statement. This helped me sooooo much! I love how you explained why I couldn't access the variable. It need a gameobject. I tried to add a vote to you but i dont have enough reputations. smh. Well THANKS
@statement,you use Gameobject.Find(),but i read on some forums that Find is not recommended by many developers because it is heavy to use,so what is it better static variable or Gameobject.Find(),any help will be appreciated.Thanks
This Q is four years old, so they probably figured it out. Back then, many examples were in javascript. And, since then, the Q has been answered dozens of times (in C#, with variations of what you wrote.) This Answer doesn't hurt, but it's fine to let old, unanswered duplicate Q's alone. In fact, deleting it might have been the better option.
This dose not seem to be working for me I just get this error: Assets\weaponSwiching.cs(27,17): error CS0029: Cannot implicitly convert type 'int' to 'bool' Note: I am a noob I could be doing something completely stupid here This is my code: GameObject.Find("first person player").GetComponent().Speed = heavyWeightSpeed;;
This point is very important and not done in Unity.
Any real time program should easily allow to share real time variables between any script.
But at the moment is is a real hassle, and never works unless you are a professionnel developer.
I have been studying for more than a year and am ashamed I canot achieve such a simple task.
Scripting is one thing, beeing a developer is another.
Game designers and non developers users feel excluded from Unity mainly because such functionalities are not simply accessible to them and not clearly explained by examples.
10 years later and sadly, this is still 100% the truth.
I don’t know if this will help, but I’ve created a single Damage script that my obstacles, good and bad and player uses. And all it has is a float for crashDamage. That’s it. Then in my individual game object main scripts that have crazy names for this bad guy, and that obstacle (having different behaviors), when I have a collision, I just search for the Damage script, get its Damage component and then minus my individual armor from that component’s crashDamage.
Using a single script with a common name makes it easy to reference with get component when my main scripts have all kinds of names. My radar picks up the closest object and then I search that object for the common name script and apply its variable to my crazy named objects if need be.
If anyone could help me, that’d be nice. So far I’ve been summoning functions from other scripts through the broadcastall functionaliy but I’d like to do it a bit more targeted. Basically what I’m trying to do is have triggers communicate their coordinates to the camera and have the camera then move to that position. When the trigger is exited, the camera should go back to following the player as it does now.
I have a script on the Zonetrigger:
public class CamZone : MonoBehaviour
{
public Vector3 Camcoordinates;
public GameObject camera = GameObject.Find("Main Camera");
// Start is called before the first frame update
void Start()
{
camera.GetComponent<CamFollow>();
}
// Update is called once per frame
void Update()
{
}
void Sendcoordinates()
{
camera.CamFollow.targetpos = Camcoordinates;
camera.CamFollow.camoverview();
}
void LeaveZone()
{
camera.CamFollow.camfollow();
}
}
and then I have the camera which has the script CamFollow
public class CamFollow : MonoBehaviour
{
public GameObject Follow;
public float maxY;
public float minY;
public float maxX;
public float minX;
public float Yoffset;
GameObject Target;
Rigidbody rbTarget;
Transform tfCam;
float camposx;
float camposy;
float camposz = -10f;
float targetx;
float targety;
Vector3 campos;
public Vector3 targetpos;
Vector3 targetvelocity;
public bool camFix = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
if (camFix = false)
{
camfollow();
}
}
private void findplayerpos()
{
tfCam = GetComponent<Transform>();
Target = Follow.gameObject;
rbTarget = Follow.GetComponent<Rigidbody>();
}
public void camfollow()
{
findplayerpos();
camposx = tfCam.position.x;
camposy = tfCam.position.y;
campos = new Vector3(camposx, camposy, camposz);
targetx = Target.transform.position.x;
targety = Target.transform.position.y + Yoffset;
targetpos = new Vector3(targetx, targety, camposz);
targetvelocity = rbTarget.velocity;
if (maxX >= targetx && targetx >= minX && maxY >= targety && targety >= minY)
{
//get camera target positions
camposx = Target.transform.position.x;
camposy = targety;
//simple follow
//tfCam.position = new Vector3(camposx, camposy, camposz);
//dampened version 1
//tfCam.position = Vector3.MoveTowards(campos, targetpos, 2f);
//dampened version 2
tfCam.position = Vector3.SmoothDamp(campos, targetpos, ref targetvelocity, 0.33f);
}
else
{
Debug.Log("out of range!");
}
//set camera position
}
public void camoverview()
{
tfCam.position = Vector3.SmoothDamp(campos, targetpos, ref targetvelocity, 0.33f);
}
}
I don’t see why this wouldn’t work but I get the error message
‘GameObject’ does not contain a definition for ‘CamFollow’ and no accessible extension method ‘CamFollow’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)’
What am I not seeing?
got it to work, if anyone wants to know, ask, this comment section is acting weird atm
Notice that the scripting reference has C#, JS and Boo examples. You can toggle which language you want to get help for by selecting from the drop down on the right side near code snippets.
– Statement