Search Unity

choosing the path in Resources.Load

Discussion in 'Scripting' started by pietrosniper99, May 15, 2022.

  1. pietrosniper99

    pietrosniper99

    Joined:
    May 8, 2021
    Posts:
    1
    Is there a way i can choose where the Orange_Object and Red_Object are being Loaded?
    Code (CSharp):
    1.     private void OnTriggerStay2D(Collider2D collision)
    2.     {
    3.         string thisGameobjectName;
    4.  
    5.         string collisionGameobjectName;
    6.  
    7.         thisGameobjectName = gameObject.name.Substring(0, name.IndexOf("_"));
    8.  
    9.         collisionGameobjectName = collision.gameObject.name.Substring(0, name.IndexOf("_"));
    10.  
    11.         if (mouseButtonReleased && thisGameobjectName == "Yellow" && thisGameobjectName == collisionGameobjectName)
    12.         {
    13.             Instantiate(Resources.Load("Orange_Object"), transform.position, Quaternion.identity);
    14.             mouseButtonReleased = false;
    15.             Destroy(collision.gameObject);
    16.             Destroy(gameObject);
    17.         }
    18.         else if (mouseButtonReleased && thisGameobjectName == "Orange" && thisGameobjectName == collisionGameobjectName)
    19.         {
    20.             Instantiate(Resources.Load("Red_Object"), transform.position, Quaternion.identity);
    21.  
    22.             mouseButtonReleased = false;
    23.             Destroy(collision.gameObject);
    24.             Destroy(gameObject);
    25.         }
    26.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I don't understand the question. Do you have multiple folders in your Resource folder?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Wow, you want to get away from that kinda coding!!

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    I would also HIGHLY recommend not doing all the string character flicking... this is 2022, not 1990. Code like the above is extremely fragile and fails in very non-obvious ways.

    Instead, stick a tag on it if that suffices, or else put a MonoBehaviour on things you need to later identify. Either have a generic identifier kinda monobehaviour with a value inside it you recognize or need, or else make specific MonoBehaviours to identify different things you care about.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums