Другие языки программирования и технологии

c# скрипт

я делаю игру в жанре traffic panic android и мне надо организовать "'общение" скриптов через переменную pressed(boolean)я загуглил, попробовал, но не работает.. .вот первый скрипт, переменную в котором надо поменять: using UnityEngine;
using System.Collections;public class carmove2 : MonoBehaviour {
public bool go;
private float posz; // Use this for initialization
void Start () {
go = false;
}

// Update is called once per frame
void Update () {
posz = transform.position.z;
if (go == false&&posz<=93.6f&&posz>=93.4f){}
else{
transform.Translate(0,0,0.2f);
};
if (posz >= 130) {
Destroy(gameObject);} }
}а вот второй скрипт, которым я меняю эту переменную; using UnityEngine;
using System.Collections;public class lightmanager : MonoBehaviour {
public bool pressed;
public carmove2 scr1;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update(){
foreach(Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)

if(pressed == true){
pressed = false;
}
else{pressed = true;};
scr1.go = pressed;
}
}
}помогите пж)
http://ideone.com/NCoznR

lightmanager1 или lightmanager2, rможет и сработает.

P.S. Лучше переменные классов не делать public, а работать с переменными снаружи
через методы или свойства.
Например public bool go { get; set; }
Zhanat Isabaev
Zhanat Isabaev
3 437
Лучший ответ
Рашид Загиров Там где вы написали //что сдесь? Не должно быть ничего. Обьект стоит на месте и ждет пока переменная go (bool) не станет = true
Рашид Загиров И можно поподробнее про это? "Лучше переменные классов не делать public, а работать с переменными снаружи через методы или свойства. Например public bool go { get; set; }"
There are several ways to achieve this.
If you want the speed variable controlled by a component which is attached to a class MyObject
public class SpeedController : MonoBehaviour
public float speed;
// maybe you want restrict this to have read access, then you should use a property insteadIn other classes you can do:
GameObject go = GameObject.Find ("MyObject");
SpeedController speedController = go.GetComponent <SpeedController> ();
float courrentSpeed = speedController.speed;Take care that there is one object named MyObject only otherwise things get messed up.
Alternatively you can define a SpeedController member in every class that needs access to speedand set a reference via drag and drop in Unity editor. You save the lookup then but of course this is pretty inconvenient if needed in many classes.
Another way is to create a singleton which holds the speed variable and have:
public class MyGlobalSpeedController {
private static MyGlobalSpeedController instance = null;
public static MyGlobalSpeedController SharedInstance {
get {
if (instance == null) {
instance = new MyGlobalSpeedController ();
}
return instance;
}
}
public float speed;
} So all classes can access this:
float currentSpeed = MyGlobalSpeedController.SharedInstance.speedAs Jan Dvorak stated in comments section:
public class SpeedController : MonoBehaviour
public static float speed;

http://stackoverflow.com/questions/13891892/in-unity-how-can-i-pass-values-from-one-script-to-another