Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetComponent not working

Discussion in 'Scripting' started by TheKillerKitten, Jun 22, 2020.

  1. TheKillerKitten

    TheKillerKitten

    Joined:
    Jun 21, 2020
    Posts:
    4
    I've looked at different posts looking for an answer but I haven't been able to find one
    This is the part of the code that matters:
    Code (CSharp):
    1.     [SerializeField]
    2.     private float _thrustForce;
    3.     [SerializeField]
    4.     private Vector3 _velocity;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         _thrustForce = GetComponent<Thrust>()._thrust;
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         _velocity += new Vector3(0, _thrustForce, 0);
    16.         transform.Translate(_velocity * Time.deltaTime);
    17.  
    18.         if (_velocity.y > _thrustForce)
    19.         {
    20.             _velocity.y = _thrustForce;
    21.         }
    22.  
    23.         EarthGravity();
    24.     }
    I just want a ship to go up darn it

    Edit: Just so you know the ship does go up if I change the serialized variable
     
  2. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    329
    And what error are u getting? null reference? can u print?

    1. Looks like u are using notation to describe private var and trying to access _thrust private field of Thrust.
    2. Thrust is a component attached to the same game object that has this script?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Why do you think GetComponent isn't working? Are you getting an error? What's happening?
     
  4. TheKillerKitten

    TheKillerKitten

    Joined:
    Jun 21, 2020
    Posts:
    4
    _thrust is a public var in a script attached to the same gameObject, there is no error in Unity, the thrust that I get in the Thrust script however does not translate into the _thrustForce in this script.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Kind of early to jump to the conclusion that GetComponent is the broken thing here. What does your Thrust script look like, and if you could share a screenshot of the inspector for this GameObject showing the fields on the Thrust script that could help too.
     
  6. TheKillerKitten

    TheKillerKitten

    Joined:
    Jun 21, 2020
    Posts:
    4
    This is the entire thrust script, it's really bare bones but I just started.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Thrust : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float _throttle = 0f;
    9.     [SerializeField]
    10.     private int _throttleMax = 100;
    11.     [SerializeField]
    12.     private int _throttleMin = 0;
    13.     [SerializeField]
    14.     private float _throttleChangeRate = 2.5f;
    15.  
    16.  
    17.     public float _thrust = 0f;
    18.     [SerializeField]
    19.     private GameObject _thrustVisual;
    20.  
    21.     [SerializeField]
    22.     private bool _isWHeld = false;
    23.     [SerializeField]
    24.     private bool _isSHeld = false;
    25.  
    26.     public int _thrusterID; // 0 = 10 thrust, 1 = 20 thrust, 2 = 30 thrust
    27.  
    28.     // Start is called before the first frame update
    29.     void Start()
    30.     {
    31.        
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         ThrustGo();
    38.     }
    39.     public void ThrustGo()
    40.     {
    41.         {
    42.             if (Input.GetKeyDown(KeyCode.W))
    43.             {
    44.                 _isWHeld = true;
    45.             }
    46.  
    47.             else if (Input.GetKeyUp(KeyCode.W))
    48.             {
    49.                 _isWHeld = false;
    50.             }
    51.  
    52.             if (Input.GetKeyDown(KeyCode.S))
    53.             {
    54.                 _isSHeld = true;
    55.             }
    56.  
    57.             else if (Input.GetKeyUp(KeyCode.S))
    58.             {
    59.                 _isSHeld = false;
    60.             }
    61.  
    62.             if (_throttle != 0)
    63.             {
    64.                 _thrustVisual.gameObject.SetActive(true);
    65.             }
    66.  
    67.             else if (_throttle == 0)
    68.             {
    69.                 _thrustVisual.gameObject.SetActive(false);
    70.             }
    71.  
    72.             StartCoroutine(ThrottleUp());
    73.             StartCoroutine(ThrottleDown());
    74.  
    75.             if (gameObject != null)
    76.             {
    77.                 switch (_thrusterID)
    78.                 {
    79.                     case 0:
    80.                         _thrust = _throttle * 0.1f;
    81.                         break;
    82.                     case 1:
    83.                         _thrust = _throttle * 0.2f;
    84.                         break;
    85.                     case 2:
    86.                         _thrust = _throttle * 0.3f;
    87.                         break;
    88.                 }
    89.             }
    90.         }
    91.     }
    92.  
    93.     public IEnumerator ThrottleUp()
    94.     {
    95.         while (_isWHeld == true)
    96.         {
    97.             _throttle++;
    98.  
    99.             if (_throttle >= _throttleMax )
    100.             {
    101.                 _throttle = _throttleMax;
    102.             }
    103.  
    104.             yield return new WaitForSeconds(_throttleChangeRate);
    105.         }
    106.  
    107.        
    108.     }
    109.  
    110.     public IEnumerator ThrottleDown()
    111.     {
    112.         while (_isSHeld == true)
    113.         {
    114.             _throttle--;
    115.  
    116.             if (_throttle <= _throttleMin)
    117.             {
    118.                 _throttle = _throttleMin;
    119.             }
    120.  
    121.             yield return new WaitForSeconds(_throttleChangeRate);
    122.         }
    123.     }
    124. }
    125.  
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Ok so _thrust starts out at 0 in that script. In your other script you're only grabbing _thrust in Start(), when it will still be 0.
     
    TheKillerKitten likes this.
  8. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    329
    @PraetorBlue, but I think that that value he's changing in the inspector, maybe he can show some inspector prints
     
  9. TheKillerKitten

    TheKillerKitten

    Joined:
    Jun 21, 2020
    Posts:
    4
    This solved it, I can't believe I'm so stupid. Sorry to everyone who replied.

    I moved the GetComponent to Update and it worked.
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    I don't think so. I think he's saying it works when he changes _thrustForce in his first script. It doesn't make much sense to change _thrust in the inspector because it's controlled by Input in the Thrust script.

    edit: yep
     
  11. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    788
    You copy only the initial value on start, float is an value type, so if you change in Thrust, the value if not changed in your other Component.

    In the other Component, safe the Thrust component reference and get the trustForce value in update:

    Code (CSharp):
    1.  
    2.     private Thrust _thrust;
    3.  
    4.     [SerializeField]
    5.     private Vector3 _velocity;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         _thrust = GetComponent<Thrust>();
    10.     }
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         _velocity += new Vector3(0, _thrust._thrust, 0);
    15.         transform.Translate(_velocity * Time.deltaTime);
    16.         if (_velocity.y > _thrust._thrust)
    17.         {
    18.             _velocity.y = _thrust._thrust;
    19.         }
    20.         EarthGravity();
    21.     }
     
  12. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    329