Search Unity

Help with GetComponent

Discussion in '2D' started by IDKWHATIMDOING124, Mar 4, 2021.

  1. IDKWHATIMDOING124

    IDKWHATIMDOING124

    Joined:
    Sep 7, 2020
    Posts:
    6
    It would seem like my GetComponent Script isn't working....

    So, I have 2 different scripts, one which is for my spikes in this case, and another which is for my lever.
    They look like this(unrelated part taken out):
    Lever:
    Code (CSharp):
    1. public class LLever : MonoBehaviour
    2. {
    3. public bool down = false;
    4. public GameObject mhm;
    5. void Start()
    6. {
    7.  
    8. }
    9. void Update()
    10.     {
    11. if (Input.GetKeyDown(interactKey) && isInRange==true)
    12.         {
    13. if(down == false)
    14.         {
    15. mhm.GetComponent<SPike>().yfs = false;
    16.         }
    17. if(down == true)
    18. {
    19. mhm.GetComponent<SPike>().yfs = false
    20. }
    21. }
    Spikes:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SPike : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     public bool yfs = false;
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.     int k = 0;
    14.     void Update()
    15.     {
    16.    
    17.     }
    18.     private void FixedUpdate()
    19.     {
    20.         Vector2 a = new Vector2(0, 10);
    21.         if (yfs == false && k==0)
    22.         {
    23.             rb.position = rb.position + a ;
    24.             k = 1;
    25.             return;
    26.         }
    27.         else if (yfs == true && k==1)
    28.         {
    29.             k = 0;
    30.             rb.position = rb.position - a;
    31.             return;
    32.         }
    33.     }
    34.  
    35. }
    36.  
    Now, when I interact with the Lever, the bool "yfs" doesn't change in the Script for the Spikes!

    Any help is welcome,
    Thanks in advence!
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    There are probably a few things, but first off, in your LLever script, you are never turning yfs to true, only false for both occasions. Also, why don't you use GetComponent in Start or Awake of the LLever script so if you get an error and it points to the Start/Awake, you know something is wrong with grabbing your reference. Then you could just do mhm.yfs = true.

    Additionally, throw in some Debug.Logs throughout both scripts so you know each area of your code is being called. Put one in Start, put one for both if(down == false) and if(down == true) in your LLever, put one under both the if and Else If statements of your SPike script.

    Lastly, this is just a general clean code practice, but clean up your class names and variable names. What is yfs? What is mfm? Why is SPike with a capital P? Why is Lever spelled like LLever? Remember, if you change the name of the class in the code, you have to change the script name in the Unity project folder too, or vice versa. Even if you are prototyping or messing around, get that stuff cleaned up and it will be much easier to read for: 1) yourself down the road or 2) For others like me trying to read it and help you. Down the road when you are doing more complicated things and you have issues, it would be very hard for me and others to discern what goes where or what is a bool and such.

    Hope this helps.