Inter Script Communication fail
Hi, I have 2 scripts I want to communicate. I Instate a prefab and then get the componet and save the reference to my second script. All I want to do it's to call a function and set some values.
enemyView.cs
public class enemyView : MonoBehaviour {
[SerializeField] public GameObject goEye;
[SerializeField] public float time = 6f;
private eyeController eyeScript;
private GameObject eye;
private GameObject canvas;
// Use this for initialization
void Start () {
canvas = GameObject.Find("FloatingCanvas");
eye = Instantiate(goEye);
eye.transform.SetParent(canvas.transform);
eyeScript = goEye.GetComponent<eyeController>();
eyeScript.attached = transform.root.FindChild("Eye");
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
if (other.CompareTag(Tags.player))
eyeScript.play(false, time);
}
void OnTriggerExit (Collider other)
{
if (other.CompareTag(Tags.player))
eyeScript.play(true, time);
}
}
eyeController.cs
public class eyeController : MonoBehaviour {
private const int margin = 32;
private static Color low = Color.green;
private static Color mid = Color.yellow;
private static Color hig = Color.red;
private Image img;
private Color currentColor = low;
private float fill = 0f;
public float speed = 0f;
public Transform attached;
private Camera cam;
// Use this for initialization
void Start () {
img = GetComponent<Image>();
img.color = currentColor;
//test
//attached = GameObject.Find("Swat").transform.FindChild("Eye");
//play(false, 10);
cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
Debug.Log(attached);
Debug.Log(speed);
img.color = currentColor;
img.fillAmount = fill;
Vector3 screenPos = cam.WorldToScreenPoint(attached.position);
screenPos = updateBounds(screenPos);
transform.position = screenPos;
increase(speed * Time.deltaTime);
}
private Vector3 updateBounds(Vector3 screenPos)
{
if (screenPos.x >= Screen.width)
{
screenPos.x = Screen.width - margin;
}
else if (screenPos.x < 0)
{
screenPos.x = margin;
}
if (screenPos.y >= Screen.height)
{
screenPos.y = Screen.height - margin;
}
else if (screenPos.y < 0)
{
screenPos.y = margin;
}
Vector3 heading = attached.position - cam.transform.position;
if (Vector3.Dot(cam.transform.forward, heading) <= 0) {
screenPos.x = Screen.width - screenPos.x;
if (screenPos.y > Screen.height / 2)
screenPos.y = Screen.height - margin;
else
screenPos.y = margin;
}
return screenPos;
}
//Incrementa el llenado del ojo y devuelve true cuando está la máximo
private void increase(float amount)
{
fill += amount;
if (fill > 1.0f)
{
fill = 1.0f;
currentColor = hig;
}
else
{
if (fill > 0.5)
{
currentColor = Color.Lerp(mid, hig, (fill - 0.5f)*2); //0.5 -> 0, 1 -> 1
}
else
{
currentColor = Color.Lerp(low, mid, fill * 2); //0 -> 0, 0.5 -> 1
}
}
}
//reverse: crece o decrece, time: tiempo que le lleva al npc verte por completo (s)
public void play(bool reverse, float time)
{
speed = 1 / time; //velocidad en unidades por segundo
if (reverse) speed = -speed;
Debug.Log(speed);
}
I want to call "play" when my player enters or exits the colliders. The function is been called but the "speed" value doesn't change, it takes the value of the first time I call "play" and never changes.
Thanks and if you need some more info let me know.
Answer by gongon123 · May 11, 2016 at 04:46 PM
My bad, I was refering the prefab instance instead of the game object instace on the line 17:
eyeScript = goEye.GetComponent<eyeController>();
eyeScript.attached = transform.root.FindChild("Eye");
Should be:
eyeScript = eye.GetComponent<eyeController>();
eyeScript.attached = transform.root.FindChild("Eye");
Your answer
![](https://koobas.hobune.stream/wayback/20220612080535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to save screenshots to com.company.product folder? 1 Answer
NullReferenceException (Updated) 1 Answer
How save data file using System.IO ? 1 Answer
Can I export soft bodies from blender into unity. 0 Answers
Saved data gets overwritten 0 Answers