Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
2.1k stars 92 forks source link

Error when trying to use .AddTo() in Unity #216

Closed guidodelphora closed 4 months ago

guidodelphora commented 4 months ago

I'm trying to use the .AddTo method as I read in the documentation but I'm getting an error.

Argument type '_Game.Features.Humans.Scripts.HumansController' is not assignable to parameter type 'System.Collections.Generic.ICollection<System.IDisposable>'

The code is the following


using R3;
using UnityEngine;

namespace _Game.Features.Humans.Scripts
{
    public class HumansController : MonoBehaviour
    {
       [SerializeField] private GameObject humanPrefab;

       private void Awake()
       {
           Observable.Interval(System.TimeSpan.FromSeconds(5))
               .Subscribe(_ => SpawnHuman())
               .AddTo(this);
       }

       private void SpawnHuman()
       {
       }
    }
}
`

I'm not sure what should I do. Thanks! 
wewrere41 commented 4 months ago

It looks like you haven't added the R3 Unity package. You can add it from the package manager https://github.com/Cysharp/R3.git?path=src/R3.Unity/Assets/R3.Unity Check the Unity section in the readme.

guidodelphora commented 4 months ago

Hey ! Thanks for the quick answer but I'm using it through all the Game so it's not that :/ Also, if it would be that, the Observable and Subscribe would also throw an error, but it's just the .AddTo

wewrere41 commented 4 months ago

Hey ! Thanks for the quick answer but I'm using it through all the Game so it's not that :/ Also, if it would be that, the Observable and Subscribe would also throw an error, but it's just the .AddTo

Yes, you can use the core API of R3 without installing the R3 Unity package. In addition to R3, there are platform-specific packages available as well. For Unity-specific features (such as the AddTo(gameobject) extension), you should also add the R3.Unity package. These are two different things. You can check the Unity section in the Readme for more details.

guidodelphora commented 4 months ago

Ohhh oh okok. I see, my fault and thanks ! At first I didn't understand but now I get it.