0
\$\begingroup\$

So, I cannot implicitly convert type object[] to UnityEngine.GameObject[]. But why? As far I know, are all types a inheritanced types of object. So, for my logic, I could put a GameObject array in a object array. That's why, this should work:

spawnedObjects = FindObjectOfType<Tools> ().AddObjectToArray (spawnedObjects, go);

//

public object[] AddObjectToArray(object[] _array, object _toAdd){
    if (_array.GetType() == _toAdd.GetType()) {
        object[] a = new object[_array.Length + 1];
        a = _array;
        a [a.Length] = _toAdd;
        return a;
    } else {
        Debug.LogWarning ("The object that should be added to the array have to be the same type as the arrays type. Initially array was retuned!");
        return _array;
    }
}

But yeah, it won't do

\$\endgroup\$
4
  • \$\begingroup\$ The way your question is worded is confusing. Is the error coming from this section of code you posted? If so exactly what line? object[] is not the same as Unity's representation of UnityEngine.GameObject[]. You make it sound like you are casting object[] into something more specific: UnityEngine.GameObject[] \$\endgroup\$ Commented Sep 13, 2017 at 20:56
  • \$\begingroup\$ the "spawnedObjects = FindObjectOfType<Tools> ().AddObjectToArray (spawnedObjects, go)" makes the problem \$\endgroup\$ Commented Sep 13, 2017 at 21:01
  • \$\begingroup\$ spawnedObjects is of type GameObject[], correct? \$\endgroup\$ Commented Sep 13, 2017 at 21:07
  • \$\begingroup\$ Are you familiar with generics? msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx \$\endgroup\$ Commented Sep 13, 2017 at 21:13

1 Answer 1

2
\$\begingroup\$

I cannot implicitly convert type object[] to UnityEngine.GameObject[]. But why?

Because allowing it subverts the type system. Assume want you want is allowed, and then consider this:

object[] a = ...;
GameObject[] b = a; 

Arrays are reference types, so a and b refer to the same actual array. Now consider if you did:

a[1] = new object();

This is legal, because a is object[]. But now b[1] is not a GameObject, it's a plain object. You have broken the type system.

Consequently, you are not permitted to make this implicit cast. It looks like you are attempting to prevent this occurrence at runtime in your code, but the compiler doesn't care. The compiler will try, and succeed, in stopping this at compile-time.


Why don't you just use List<GameObject>? It will let you append new GameObjects to the list more efficiently than you're doing by recreating the entire array (and with fewer bugs). For example:

List<GameObject> spawnedObjects;

...

spawnedObjects.Add(go);
\$\endgroup\$
3
  • \$\begingroup\$ Ahh, I understand the whole problem. Thank you :) 👍 \$\endgroup\$ Commented Sep 13, 2017 at 21:18
  • \$\begingroup\$ I wanted to try something different this time \$\endgroup\$ Commented Sep 13, 2017 at 21:29
  • \$\begingroup\$ Sure, that's fair, but what you have is slower, less memory efficient, and will crash since you access an array out of bound ("a[a.Length]" isn't safe) \$\endgroup\$ Commented Sep 13, 2017 at 21:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.