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 Answers 2

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.

\$\endgroup\$
0
\$\begingroup\$

Resolved by changing FindObjectOfType to FindObjectsOfType:

var listeners = FindObjectsOfType<MonoBehaviour> ().OfType<iPlayerRespawnListener> ();
\$\endgroup\$
1
  • \$\begingroup\$ Why are you not just looking for objects of type iPlayerRespawnListener? Are there non-MonoBehaviours that have it as a component? \$\endgroup\$ Commented Sep 4, 2014 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .