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
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
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
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.
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
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.
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.
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.
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?