Search Unity

The script doesn't to space being pressed anymore, AND, can't manage alpha of canvas.

Discussion in 'Scripting' started by Temp10101, Mar 26, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.  
    5. class CameraFollow : MonoBehaviour {
    6.     private Vector3 currentPosition;
    7.     public GameObject Player;
    8.     public Transform ZoomOutTo;
    9.     // public GUIManager GUIToggler;
    10.     float zDistance;
    11.  
    12.     public float speed = 1.0f;
    13.  
    14.     public Canvas THECanvas;
    15.  
    16.     private float zoomOutStartTime;
    17.     private float ABDistance;
    18.  
    19.     public float zoomedIn = 1.73f;
    20.     public float zoomedOut = 4.5f;
    21.  
    22.     public bool zoomed = false;
    23.    
    24.     void Start() {
    25.         currentPosition = transform.position;
    26.         zDistance = currentPosition.z - Player.transform.position.z;
    27.  
    28.         ABDistance = Vector3.Distance(transform.position, ZoomOutTo.position);
    29.  
    30.         camera.orthographic = true;
    31.     }
    32.    
    33.     void Update() {
    34.         Vector3 toPosition = new Vector3(Player.transform.position.x, Player.transform.position.y, Player.transform.position.z + zDistance);
    35.         transform.position = toPosition;
    36.  
    37.         if (Input.GetKeyDown(KeyCode.Space)) { zoomed = !zoomed; }
    38.  
    39.         if (zoomed) {
    40.             THECanvas.GetComponent<UnityEngine.UI.Image>().CrossFadeAlpha(0f, 1f, false);
    41.             camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, 4.5f, Time.deltaTime);
    42.             if (camera.orthographicSize >= 4) { Time.timeScale = 0.0001f; }
    43.         }
    44.         else {
    45.             THECanvas.GetComponent<UnityEngine.UI.Text>().CrossFadeAlpha(1f, 1f, false);
    46.             camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, 1.73f, Time.deltaTime);
    47.             Time.timeScale = 1;
    48.         }
    49.     }
    50. }
    Pressing Space doesn't give any effect, while it did before. AND, I can't manage Canvas to disappear because of these two errors.

    Same error on line 40.
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Have you dragged the object you want into the Canvas field in the editor ? Either do that or Find the object and set the reference in code in your Start function.
     
  3. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54


    Did I do it wrong? Sure "Zoom Out To", is also empty. But it's not line the Unity is referring to, Unity is referring to lines where I use THECanvas as an object, he can't handle it. Except "THECanvas" script is dependant right?
     
  4. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    If the Canvas is set there, then it must be the GetComponent.

    I haven't used the new unity gui yet but it looks like the elements you are referencing will be children of the canvas.

    Either use GetComponentInChildren<>() or more sensibly store a reference to your image and text game objects and access them using that. If you have more than one image or text child in your GUI (which is likely) you will want to be able to reference them all individually.
     
  5. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    So it becomes:

    THECanvas.GetComponentsInChildren<UnityEngine.UI.Image>().CrossFadeColor(Color.black, 2.0f, false);
     
  6. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    That should work, but only if you have one Image on your canvas. Not ideal.

    It's better if you store a reference to your image like you do with the canvas.

    Then reference the image directly.
     
  7. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    THECanvas.GetComponentsInChildren<UnityEngine.UI.Image>().CrossFadeColor(Color.black, 2.0f, false);
    That should work, but only if you have one Image on your canvas. Not ideal.


    The "CrossFadeColor" is red, that means an error. I colored the text as my editor shows me. It wasn't solution I came up with.

    Error being:
    Assets/Scripts/HPThing.cs(17,60): error CS1061: Type `UnityEngine.UI.Image[]' does not contain a definition for `CrossFadeAlpha' and no extension method `CrossFadeAlpha' of type `UnityEngine.UI.Image[]' could be found (are you missing a using directive or an assembly reference?)

    I also changed from: using UnityEditor.UI; to using UnityEngine.UI;
     
  8. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  9. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    This is exactly what I had, I just copied the line from link you provided, changed it to my use, but it´s exactly same code that is now in commented chain.
     
  10. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  11. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    How do I even get data from a

    THECanvas.GetComponentsInChildren<Image>().CrossFadeColor(Color.black, 2.0f, false);

    This isn't a variable, this is calling a function. How do I get array from "Execute this".?