- Home /
How to pass float from one script to another in Unity 5? C#
I'm working in Unity 5 Personal edition.. I'm trying to pass float from one script to another, but I get this error:
Assets/CameraTracksPlayer.cs(15,36): error CS1061: Type
ClimberMovement' > does not contain a definition for >
stopCamera' and no extension methodstopCamera' of type
ClimberMovement' could be found (are you missing a using directive or an assembly reference?)
I don't know what am I doing wrong.
Here is my code:
using UnityEngine;
using System.Collections;
public class CameraTracksPlayer : MonoBehaviour {
Transform player;
float isDead;
float offsetY;
// Use this for initialization
void Start () {
GameObject go = GameObject.Find ("MainCamera");
ClimberMovement dying = go.GetComponent <ClimberMovement> ();
float died = dying.stopCamera; //this line gets error!
isDead = died;
GameObject player_go = GameObject.FindGameObjectsWithTag("Player")[0];
if (player_go == null) {
Debug.LogError("Ne dela ker ni taga Player");
return;
}
player = player_go.transform;
offsetY = transform.position.y - player.position.y;
}
// Update is called once per frame
void Update () {
if (player != null && isDead == 1) {
Vector3 pos = transform.position;
}
else if (player != null) {
Vector3 pos = transform.position;
pos.y = player.position.y + offsetY;
transform.position = pos;
}
}
}
This is the code of the script from which I am trying to pass variable:
using UnityEngine;
using System.Collections;
public class ClimberMovement : MonoBehaviour {
...
public float stopCamera = 0f;
...
if (transform.position.x <= -3.9f) {
transform.position = new Vector3 (-3.9f, transform.position.y, transform.position.z);
if (transform.position.x == -3.9f) {
dead = true;
stopCamera = 1f;
animator.SetTrigger ("Death");
velocity.y = -5f;
velocity.x = -5f;
}
} else if (transform.position.x >= -1f) {
transform.position = new Vector3 (-1f, transform.position.y, transform.position.z);
}
}
}
Your error message looks a little odd: it has some ">" characters in odd places: > does not contain a definition for > stopCamera'
Are you sure you haven't got a typo in your original code somewhere - perhaps got too many > on line 14?
You should change
GameObject go = GameObject.Find ("$$anonymous$$ainCamera");
by
GameObject go = Camera.main;
I suspect the "stopCamera" is inside a Sub-Class that's why the compiler can't see it.
Answer by Fruhlicht · Jun 06, 2015 at 06:28 AM
You can only pass static vars, so change public float stopCamera = 0f; into public static float stopCamera = 0f;
This is just nonsense. https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
tanoshimi is correct statics are bad if you are using them that way. static members tend to linger in memory because GC doesn't know when they are going to be referenced next(even if they are NO instances of the class!). thus they are only removed from memory(yes even long after using Object.Destroy) when your game stops running.
Another thing is that static member will be always the same on all instances because static members do not rely the instance. e.g if staticVar = 6, and you have 3 class instances that have that same variables and you change it to 7 on one of them, they will all be 7.
static memebers are best used for constant or global members that wikll be needed throughout the entire game. e.g you could have a static instance of a "SystemData" class that will track the game time, unlock or settings and be able to accessed from various UIs.
to access normal instance variables of a class, find an instance of that class(see tanoshimis link) or create one with the new
keyword (if it inherits from $$anonymous$$onoBehaviour use AddComponent)
Answer by Ilgiz · Jun 05, 2015 at 01:53 PM
Try to write your ClimberMovement code like this:
using UnityEngine;
using System.Collections;
public class ClimberMovement : MonoBehaviour {
public float stopCamera = 0f;
}