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

Get AssetReference key

Discussion in 'Addressables' started by Kozaki2, Oct 1, 2020.

  1. Kozaki2

    Kozaki2

    Joined:
    Apr 8, 2019
    Posts:
    47
    Hi, how can I get the key of the AssetReference asigned in inspector? I want to instantiate on scene such asset but depending on what that asset is, the position and rotation will be different.

    I want do something like this but RuntimeKey is not for my need so the script below works wrong
    Code (CSharp):
    1.  
    2. public AssetReference dropItem;
    3. (...)
    4. if (dropItem.RuntimeKeyIsValid())
    5.                 {
    6.                     Vector3 position;
    7.                     Quaternion rotation;
    8.                     switch (dropItem.RuntimeKey)
    9.                     {
    10.                         case "Item1":
    11.                             position = new Vector3(transform.position.x, 1, transform.position.z);
    12.                             rotation = Quaternion.Euler(90, 0, 0);
    13.                             break;
    14.                         case "Item2":
    15.                             position = new Vector3(transform.position.x, -0.24f, transform.position.z);
    16.                             rotation = Quaternion.Euler(-90, 0, 0);
    17.                             break;
    18.                         default:
    19.                             throw new ArgumentOutOfRangeException();
    20.                     }
    21.                  
    22.                     dropItem.InstantiateAsync(position, rotation);
    23.                 }