Access GameObjectList from Another Script & change the Game Object to = ListObject

I have set up instantiated Gameobjects that are added to a LIST. Now I just need to attach my List to the spawner.

SpawnerGameObject = GameObjectLIST

Picking from a list in script to assign the value of Game Object

-I have made a List with my cloned GameObjects

  • I have a spawner that selects through a list by making an ONCLICK button to change what the spawner is equal to

  • For example Spawner = square. If I click Circle Button, then Spawner is = circle Object

  • What do I do if I have made a public list in another script? So Spawner is = Triangle(List-ranging from 1-20 triangles).

  • Game Object = GameObject List

  • I am happy if it can cycle through 1-20 each time I click a button. 1, 2, 3,4,5…….1,2,3,4,5,

  • Or increase with one button 1,2,3,4 and decrease with another 1,2,3,4

  • Something similar to Game Object = GameObject.FindwithTag “…….

I also need Access the list inside from a different script unity c# (also different Game Object)

Your help is greatly appreciated,

You won’t get much help without posting some kind of useful code and explaining where you’re having the difficulty.

If you have no code yet, I suggest working through some relevant tutorials on enemy spawning… youtube is cram-packed with tons of this stuff, so there’s no sense in wasting time with anything abstract: go do some actual tutorials and end up with actual functioning code.

Hi Kurt,
Sorry that I missed the code details needed.
I have the code.
So this is where my sound spawner switches to another sound object prefab with a click of a button.
Here, my GameObject that is instantiated has been made.

private GameObject spawnedSoundObject;

public void SetSoundPrefabType(GameObject soundprefabType)
{
    _drawObjectPrefab = soundprefabType;

}

The Instantiation list happens on another script

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

I need to know how I can call my ‘_recordinglist’ Game object variable to
be picked from the

_drawObjectPrefab = …

The general case of reaching into another script instance is that you need a reference to that script instance.

ScriptA, if it wants to reach into VariableB inside of ScriptB, will need a local reference to the instance of ScriptB that you want.

Usually this is done by making a public field in ScriptA and dragging the specific instance you want of ScriptB into that slot, which is basically how almost everything is normally hooked up in Unity3D.

The scripts are not instances. The spawner has the Spawn object which can be swtiched
And an Empty Object holds the Sound Script which has the List.

My Goal is to

  1. Call the Game Object List from the Empty Object with a Sound Script
    2.Change the GameObject to be spawned in the spawn object script to become the Game Object List in the Empty Object
    3.Switch through the list of Game Objects

Hopefully that makes more sense?

This makes no sense. You don’t “call game object lists.” You call methods on single objects, optionally with arguments.

This also does not make sense. You can’t turn a single GameObject into a list of GAmeObjects. You can add a GameObject to another list of GameObjects.

What does this mean? Iterate through the list? If so, use a foreach loop and consider each object in the list and do something, either call a method on it, or pass it to some other method, or make some kind of decision about it…

It might be best to back up here a bit and work on some other unrelated tutorials with a mind to understanding the typical relationships between GameObjects and the components attached to them, and to understand what objects contain methods and properties and what those things can do for you.

For instance, List contains an Add() method to let you add things to it.

For another instance, an AudioSource contains a property you can put an AudioClip into, and a method called Play() to make it play.

Unless you can properly communicate your intentions to humans, it is unlikely you can do so for the computer, which is far more exacting.

1 Like

What I mean is like calling a an apple from a pile of apples (each apple has it’s own number) instead of a single apple. It must be possible to do this?

Instead of saying Apple = pear

I’m saying. Apple = pear 1 from a List of pears

Then it can switch to Pear 2, 3, 4 within that specific pear list

EDIT: I mean the ability to pick from a list of “pears” in the pile and I will make the GameObject in the other script equal to that.
Game Object = GameObject 1 from GameObject list

And yes I have already created a list successfully today with Add()
as I wrote in the first part “I have made a List with my cloned GameObjects”
Since I’ve used NatDevice Plugin for record, I couldn’t just paste their code on here as it would be confusing. If this last explanation doesn’t make sense, then I think I may need some unrelated tutorials on Explaining things to humans haha

Are you asking how to dereference a variable in a list? It’s just like an array, same syntax, the square brackets:

// for either type, which obviously must be initialized:
List<GameObject> myList;
GameObject[] myArray;

You access the first one (if it exists) with:

Debug.Log( myList[0]);   // zero is always the first
Debug.Log( myArray[0]);   // zero is always the first

Lists use a .Count property that returns how many are in them.

Arrays use a .Length property to do the same.

It needs to be a List, since the number of items will change at run time.
adding ‘myList[0]’ will call the list that I want my Game Object to become?

EDIT:

I think I’m looking into For Each Loops and it does something close to what I’m looking for

You can use the FindObjectOfType method (Link to the doc at the beginning) to look for the script that has the recording list and then assign it to a variable to access it from that script. Keep in mind recordingList should be a global and public variable for this to work.

Also you cannot set an array to a list. You should first make sure they are the same type then use the .ToArray() method to convert a list into an array. The array however should be the length of the list, so you can use the .Count variable to get the length of a list, as Kurt said.

Sorry if this is not what you were looking for.

Hi

The goal is to be able to

  1. access objects in the List , I have created
  2. Make My GameObject in Another script = one of the objects in that list
  3. Be able to cycle/switch the different recordings

The second script has my spawner where I instantiate the recordings.

Okay, you should be able to do all of that with the information in this forum.

You can access the list using FindObjectOfType.

You can get one of the objects from a list using just square brackets and an index (starting at 0), as Kurt said.

I don’t know what you mean by cycling or switching, but you should just be able to edit the list, by removing and adding elements. Just make sure that while you are changing the list, you shouldn’t try accessing an element from another script, because it will cause bugs.

Yes, I tried looking at the link, but it just shows the settings for a camera.

Do I just replace the name Camera with my List Variable name?

No, the FindObjectOfType method takes a class, not a variable. As the docs say, it shouldn’t be called every frame, so make a reference to it in the Awake or Start event, and then just access the recording list in Update or another function.

Yes that’s the question I’m asking, is how is this written out? Is there any example anywhere?

It works fine by calling an object with a name tag, but I don’t know how we setup for different objects on a list ? _drawObjectPrefab = GameObject.FindGameObjectsWithTag("RecordedCube")[0];

I would assume there are people who, make a game where they want to select through a list of weapons on their character which will spawn the bullets from the list?