-1

I trust that there are amazing people here that can solve this problem

I have a List of GameObjects that have been made in my first script;

public List<GameObject> _recordinglist = new List<GameObject>();

Then a button creates a clone and adds to the _recordinglist

      recordedObject = Instantiate(mynewPrefab, new Vector3(0, 0, 0), Quaternion.identity);
// adding to created list 
_recordinglist.Add(recordedObject);

Now in the second Script

I have a working script that

changes the GameObject into other GameObjects. E.g. Turning a Kick into a Clap with the click of different buttons where I would change the Game Object in the Inspector

However

Trying to do

GameObject = List[0];

Gives Errors even though it is initialised

Also I'm not sure how to click/cylce through the list with a button?

Some clear guidance for a relatively beginner coder would be greatly appreciated.

I have already asked in the Unity Forum, but it lead to confusion. I have also emailed directly and the answers aren't detailed enough. I have also checked around and haven't seen exactly what I need.

In other words,

I'm Trying to make

Recording1 = Recording2 or 3, 4 , 5, 6, then start at 1 as I select through.

Instead of just Recording = Recording 2

Vinni Marcon
  • 606
  • 1
  • 4
  • 18
  • Does this answer your question: [Is it possible to: increment index by 1 and wrap back to 0 if(index > list.Count) within 1 line of code?](https://stackoverflow.com/questions/42459255/is-it-possible-to-increment-index-by-1-and-wrap-back-to-0-ifindex-list-count) ? – derHugo Dec 27 '20 at 19:49
  • Thanks for this part! I also forgot to say I keep getting this error. Even with your code. I even tried and it still gives a errors "Cannot implicitly convert type 'NatSuite.Examples.HotMic' to 'UnityEngine.GameObject'". Do I need to make a copy of the script in this second one to work? – – Zachary Aghaizu Dec 28 '20 at 08:52
  • Completely unclear what you are doing there ... what is a `NatSuite.Examples.HotMic` ?? .. do you maybe forget to get the `.gameObject` somewhere? – derHugo Dec 28 '20 at 10:03
  • @derHugo Sorry, the NatSuite script is from a unity plugin script. I basically used it to record and the list was something I added to create GameObjects with the new Audio. The second script works well with `code _drawObjectPrefab = GameObject.FindGameObjectsWithTag("RecordedCube")[0];` but calling the list. gives that error. The list is already a game object in the first script(HotMic script) `code public List _recordinglist = new List();` – Zachary Aghaizu Dec 28 '20 at 11:35
  • `code public void SetRecPrefabType(GameObject[] _recordinglist) { _drawObjectPrefab = _recordinglist[0]; }` is the closest I've gotten so far with no errors, but the button Onclick stops showing the function once GameObject[] instead of Gameobject is used – Zachary Aghaizu Dec 28 '20 at 14:01

1 Answers1

0

Not sure I understood, but I think this is what you want:

GameObject r = _recordings[0]

(In the question you forgot to give the variable a name)

to iterate through the list:

int i = 0;
GameObject go = _recordings[i];
i = (i + 1) % _recordings.Length;

The modulus (%) symbol returns the division remainder, so it makes sure i would just be set back to 0 when it gets to _recordings.Length

SagiZiv
  • 932
  • 1
  • 16
  • 38
  • If it's a `List` it would rather be `_recordings.Count` though ;) – derHugo Dec 27 '20 at 19:51
  • Thanks for this part! I also forgot to say I keep getting this error. Even with your code. I even tried and it still gives a errors "Cannot implicitly convert type 'NatSuite.Examples.HotMic' to 'UnityEngine.GameObject'". Do I need to make a copy of the script in this second one to work? – Zachary Aghaizu Dec 28 '20 at 08:51
  • @SagiZiv Thanks for your answer, I keep getting the error that "Cannot implicitly convert type to'UnityEngine.GameObject'". the NatSuite script is from a unity plugin script. I basically used it to record and the list was something I added to create GameObjects with the new Audio. The second script works well with `code _drawObjectPrefab = GameObject.FindGameObjectsWithTag("RecordedCube")[0];` but calling the list. gives that error. The list is already a game object in the first script(HotMic script) `code public List _recordinglist = new List();` – Zachary Aghaizu Dec 28 '20 at 11:39
  • `code public void SetRecPrefabType(GameObject[] _recordinglist) { _drawObjectPrefab = _recordinglist[0]; }` is the closest I've gotten so far with no errors, but the button Onclick stops showing the function once GameObject[] instead of Gameobject is used – Zachary Aghaizu Dec 28 '20 at 14:01