Home > Mobile >  "cannot convert from gameObject to Transform" I think its a simple fix but I'm still
"cannot convert from gameObject to Transform" I think its a simple fix but I'm still

Time:02-25

I'm in the process of writing the c# code for the multiplayer that's being implemented into my game me and my friends are working on and I've run into the following error: "Assets\Scripts\Multiplayer\Launcher.cs(99,43): error CS1503: Argument 2: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.Transform'"

I believe its complaining about these lines of code which are supposed to control part of the multiplayer lobby:

public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
    foreach(Transform trans in roomListContent)
    {
        Destroy(trans.gameObject);
    }
    for(int i = 0; i < roomList.Count; i  )
    {
        Instantiate(RoomListItemPrefab, roomListContent).GetComponent<RoomListItem>().SetUp(roomList[i]);
    }
}

public override void OnPlayerEnteredRoom(Player newPlayer)
{
    Instantiate(PlayerListItemPrefab, playerListContent).GetComponent<PlayerListItem>().SetUp(newPlayer);
}

}

CodePudding user response:

Such error message simply means you are passing an argument of the wrong type. In this case most likely it's complaining the second arg to Instantiate() it should be a Transform but is a GameObject. Hard to say for sure because you didn't even post the full source code. What's the type of playerListContent ?

CodePudding user response:

As said by gregee, it is hard to know exactly the problem here but it is likely that playerListContent is not of type gameObject. When giving instantiate two parameters, as you are, they must fit specific types that will change based on the total number of parameters you are giving. In this case:

Instantiate(Object original, Transform parent)

Meaning that playerListContent should be a transform to parent the instantiated object too.

  • Related