Where to put my list of instantiated prefabs?

Hello,
I’m very new to Unity. I used to program in other languages a bit, but here I’m at a loss right now.
I have a player model, das shoots bullets.
Those bullets are instances of a prefab.

Now I want to save those instances in a List, that seems to work.
But when I want to access a variable (moved) that is part of the prefab, I can’t.

Where I see the problem:
I have a monobehaviour scrpt for my player, that’s where the bullets are created.
Then I have a monobehaviour script for my bullets.
But, if I put the list in the bullet script, it wouldn’t work, right?
I have the list in my player script, but it can’t access the moved var.

I’m at a loss when it comes to those GameObjects: Does each gameobjects’ script run each update()?
Why is it, when I write in the script of the bullet, that I want to destroy it, that it destroys the main prefab, and not just the instance?

Yes, I skipped tutorials so far, hoping I can figure it out somehow, slowly thinking I shouldn’t :confused:

Maybe someone unterstands my issues and can help? Thank you very very much in advance!

It sounds like you’re creating a list of references to the bullet prefab rather than to each newly-created bullet instance.

The reply to this topic shows how to properly store a reference to an instance upon instantiation, and then add that reference to a list:

As for the “moved” variable, if it isn’t public then it can’t be accessed from external classes. You can either make it public or write a public method (known as a “setter”) that allows you to manipulate it indirectly.

1 Like

just curios why you want to keep track of the bullets. why you add them to the list?

this is not clear.

yes, this is called the game loop, unity will access and run all these game objects update code. the computer nowdays have enough power so is not noticeable except if you do some crazy code that requires resources that can halt the cpu or gpu.

you can find more about unity loop by stuying this diagram

First of all: Thanks for the kind help, I wasn’t expecting people being nice about this.
I did scrape a few dozen threads, but the one you linked actually helped - finally!

@altepTest:
Keep track of bullets, because: I want several different behaviours, colors e.t.c, and for starters I like to see exactly what I’m doing. I’m sure, after a while I don’t have to track them anymore and can initialize them with the properties I need.

The diagram you linked seems to be super helpful, I will check that in detail later!

All in all, I made it work for now, dirty and not sure how, so yea, I have a lot more reading to dog

this is a good book to learn how to structure your code in unity, for example you can let the bullets do their own code, change their options based on some rules and other stuff like that. sooner you get familiar with the techniques in this book, better it is. it will save you a lot of time in the future.