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