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.

Question How do I getcomponent type script base on int of that scripts?

Discussion in 'Scripting' started by Zuc16th, Sep 4, 2023.

  1. Zuc16th

    Zuc16th

    Joined:
    Apr 22, 2022
    Posts:
    8
    I having a question. I have 1 player(gameobject) and many stair(gameobject). Each stair contain their own stair_condition_scripts, the inside are the same except the int call stair_id in each scripts.

    the systems I have right now are working like this: when player reach that stair it will detect if that stair has a tag call stair_entrance or not. Then it will getcomponent<stair_condition_script>(). Now the problem occur when I reach another stair. The player script can't detect which one of this stair was it, and it can't get a new stair_gameobject_scripts of that new stair. So now I have an idea of give each stair an integer of it own so the player script could defy it. But another question come up. Now how do I detect the intiger of that scripts and how do I getcomponent of that exact stair?

    Or does anyone has a better solution of this?
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm assuming you use a raycast down to get said stair you're on? Then yes, ray down, get the collider, the get stair component from collider, then just check what integer it is.

    I'm also having trouble understanding why the player would need to know the stair it's on? But I assume you have certain logic that runs per type of stair. So instead of an integer, you may be better off using enums(as they can be used as names and numbers).

    So just a quick thought of how to type that out:
    Code (CSharp):
    1. public class Stair : MonoBehaviour
    2. {
    3.     public int myTypeInt; // 7?
    4.    
    5.     public enum StairType
    6.     {
    7.         None,
    8.         Entrance,
    9.         Exit,
    10.         Middle,
    11.         // etc...
    12.     }
    13.     public StairType stairType;
    14. }
    15.  
    16. public class Player : MonoBehaviour
    17. {
    18.     public Stair currentStair;
    19.    
    20.     void CheckStair()
    21.     {
    22.         if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hitinfo))
    23.         {
    24.             if (hitinfo.TryGetComponent<Stair>(out currentStair))
    25.             {
    26.                 if (currentStair.myTypeInt == 7)
    27.                 { DoLogicForCurrentStair(); }
    28.                
    29.                 if (currentStair.stairType == Stair.StairType.Middle)
    30.                 { DoMiddleStairLogic(); }
    31.             }
    32.         }
    33.     }
    34. }
    I quickly wrote this in NotePad++, so might have errors, test and play around with it. :)
     
    Zuc16th likes this.
  3. Zuc16th

    Zuc16th

    Joined:
    Apr 22, 2022
    Posts:
    8
    I did try this:

    Code (CSharp):
    1.  
    2.  
    3. public class raycast_check : MonoBehaviour
    4. {
    5.     public LayerMask Ground_Layer;
    6.     public Rigidbody2D rb2d;
    7.     private void Start()
    8.     {
    9.         rb2d.GetComponent<Rigidbody2D>();
    10.     }
    11.     private void Update()
    12.     {
    13.         drawLine_();
    14.         CheckRayCast();
    15.     }
    16.     void CheckRayCast()
    17.     {
    18.         //Debug.Log("raycast func, is working");
    19.         if (Physics.Raycast(rb2d.transform.position, Vector3.down, out RaycastHit hitinfo, 1.5f, Ground_Layer))
    20.             {
    21.             Debug.Log(hitinfo.transform.name);
    22.         }
    23.     }
    24.     void drawLine_()
    25.     {
    26.         Debug.DrawRay(transform.position, Vector3.down * 1.5f, Color.red);
    27.     }
    28. }
    29.  
    Debug.log return nothing. I wonder where did I goes wrong. I also ask other around but no one understand what is going on.
     

    Attached Files: