1

I have been trying to solve this problem for many hours now, but i just cant figure it out.

I need to access the array "letters in the voids "nextItem" and "prevItem", but i get an error saying "ArgumentException: GetComponent requires that the requested component 'GameObject[]' derives from MonoBehaviour or Component or is an interface."

using UnityEngine;
using System.Collections;

public class buttons_abc : MonoBehaviour {
public int id;
public GameObject[] letters;

// Use this for initialization
void Start () {
    id = 0;
    GameObject[] letters = GameObject.FindGameObjectsWithTag ("letter");
    letters[id].SetActive (true);
    for (int i = 1; i < 32; i++) {
        letters[i].SetActive (false);
    }

}
public void nextItem(){
    letters = GetComponent<GameObject[]>();
    Debug.Log (id);
    if(id < 32){
        letters[id].SetActive (false);
        letters[id + 1].SetActive (true);
        id++;
    } else {
        Debug.Log("viimane t2ht");
    }
}
public void prevItem(){
    letters = GetComponent<GameObject[]>();
    Debug.Log (id);
        if(id > 0){

            letters[id].SetActive(false);
            letters[id-1].SetActive(true);
            id--;
        } else{
            Debug.Log("esimene t2ht");
        }

} }

2 Answers 2

1

Too many wrong things in your code.

  1. Declaring GameObject named letters then re-declaring it in the Start() function again.

  2. letters = GetComponent<GameObject[]>(); Overwriting the current GameObject reference?

  3. GetComponent<GameObject[]>(); You can't do this.(Your main error)

  4. 4.

for (int i = 1; i < 32; i++) { letters[i].SetActive (false); }

You have no way to check if your array is out of bound. i< 32 should be letters.Length

Sign up to request clarification or add additional context in comments.

Comments

0

There's a few errors in your script.
1. There's no GetComponent that can return a GameObject EVER. Period. The GetComponent function is used to get a SINGLE component attached to a GameObject.
2. That being said, GetComponent cannot return Arrays either. The type you're looking for GetComponent<GameObject[]> is invalid. Valid GetComponents calls look like this

GetComponent<AudioSource>();
  1. You have a global array of GameObjects called letters. You're then creating ANOTHER array in Start, this time locally, calling it the same thing (letters).

Putting it all together

using UnityEngine;
using System.Collections;

public class buttons_abc : MonoBehaviour {
public int id;
public GameObject[] letters;

// Use this for initialization
void Start () {
    id = 0;
    //Already declared letters globally. Creating another one here locally will mean that your global array will not be populated
    //GameObject[] letters = GameObject.FindGameObjectsWithTag ("letter");
    letters = GameObject.FindGameObjectsWithTag ("letter");
    letters[id].SetActive (true);
    for (int i = 1; i < 32; i++) {
        letters[i].SetActive (false);
    }

}
public void nextItem(){
    //GetComponent only works on Components. GameObjects are NEVER components
    //GetComponent cannot return an array
    //letters = GetComponent<GameObject[]>();
    Debug.Log (id);
    if(id < 32){
        letters[id].SetActive (false);
        letters[id + 1].SetActive (true);
        id++;
    } else {
        Debug.Log("viimane t2ht");
    }
}
public void prevItem(){
    //See comment from nextItem()
    //letters = GetComponent<GameObject[]>();
    Debug.Log (id);
        if(id > 0){

            letters[id].SetActive(false);
            letters[id-1].SetActive(true);
            id--;
        } else{
            Debug.Log("esimene t2ht");
        }
} }

1 Comment

Thank you very much, if i hadn't double declared it in the first place, i wouldn't had any problems. I started messing with the getComponent after the first error. But it is fixed now, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.