Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to convert a string to the address of an object?

Discussion in 'Addressables' started by Lesnikus5, Apr 9, 2020.

  1. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    I make a save/load system for the game. I am trying to serialize all the data of an object, including its address in the Addressables system. When deserializing, I get this address to load the desired prefab through the Addressables system.

    As an address, I use the name of the object. That is, the prefab name and address are the same. Ideally, I could use something like Addressables.GetAddress (myObject), but, as far as I understand, nothing of the kind is provided for. Therefore, i have to use the name as the address.

    I serialize the name to string. But when I try to create an object on the scene, an error appears:

    error CS1503: Argument 1: cannot convert from 'string' to 'UnityEngine.ResourceManagement.ResourceLocations.IResourceLocation'

    I load the object like this (objData.prefabAddress is a string with the name of the prefab, for example, "Vehicle1"):

    Code (CSharp):
    1. Addressables.InstantiateAsync (objData.prefabAddress, pos, rot) .Completed + = handle =>
    2. {
    3.     InitObject (handle.Result, objData);
    4. };
    Maybe I'm working not correctly with Addressables at all. How to serialize the prefab address in order to spawn it later?
     
    Deleted User likes this.
  2. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    I will formulate the question easier: how to serialize the prefab address to use it in the game save / load system?
     
  3. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    In general, I decided to abandon Addressables and switched to Resources.Load ("prefab path"). String is accepted without problems, I can serialize the path to the prefab to the save file as a string, and Addressables is some kind of confused system that does not allow me to do this. It is especially harmful that the Scripting API is scanty, and the description is not always clear and there are no examples.