0
\$\begingroup\$

This is the line where I am getting above mentioned error:

var listeners = FindObjectOfType<MonoBehaviour> ().OfType<iPlayerRespawnListener> ();

I have already imported these libraries including System.Linq which has the above mentioned method OfType:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;

Where is it going wrong and how to fix this?

\$\endgroup\$
2
\$\begingroup\$

To expand on OP's answer: the crucial difference is the use of FindObjectOfType versus FindObjectsOfType.

  • FindObjectOfType returns a single object.
  • FindObjectsOfType returns an array of objects.

Linq's OfType extension method requires an enumerable collection (such as an array), which is why you were seeing that error and why switching calls fixed it.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Resolved by changing FindObjectOfType to FindObjectsOfType:

var listeners = FindObjectsOfType<MonoBehaviour> ().OfType<iPlayerRespawnListener> ();
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Why are you not just looking for objects of type iPlayerRespawnListener? Are there non-MonoBehaviours that have it as a component? \$\endgroup\$ – BennyMathison Sep 4 '14 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.