Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by gongon123 · May 10, 2016 at 07:17 PM · unity 5cs

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges