Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

RectTransform could not be found, missing directive?

Discussion in 'Scripting' started by denholmspurr, Apr 23, 2020.

  1. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    I'm trying to use RectTransform to manipulate my UI, but my code isn't recognising it. Am I missing a "using..." function or something?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5. using UnityEngine.UI;
    6.  
    7. public class MovePanel : MonoBehaviour
    8. {
    9.  
    10.     float timeOfTravel = 5; //time after object reach a target place
    11.     float currentTime = 0; // actual floting time
    12.     float normalizedValue
    13.     RectTranform rectTransform = GetComponent<RectTransform>(); //getting reference to this component
     
  2. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Bah! Idiot. Spelling misstake! o_O Rookie

    So i fixed the RectTransform. That's not recognised, but the GetComponent<RectTransform> is not. Any ideas?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You can't call GetComponent in a class constructor like that. The class instance gets created by some other loading thread.

    You can make it a lazy getter if you want.
     
  4. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Ah ok, thanks.

    I ended up adding the DoTween asset to help me move UI objects. Godsend!
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Brilliant! Use trusted code that everybody else is already using. Why hassle with reinventing the wheel?
     
    denholmspurr likes this.