I have a public gameobject with a prefab inside, using it as Particle System.
I instantiate it in Awake with the intention to set it inactive and only setactive when collisions happen.
But the instantiated gameobject is only recognized in Awake, not in other methods.
I found some threads where people are recommending it this way, so it should work?
The instantiated gameobject is existing, I can parent it to the character gameobject.
But nevertheless, I can’t “get it back”.
Not even with find().
Am I missing something? Or this is maybe not possible (anymore)?
You’re trying to reference something out of Scope. Update() can’t see what you did in Awake() so it has no idea what you’re talking about and thus cannot compile.
You’ve declared a local object named “go” in your Awake function, and that’s what you’ve set to point to the new spawn. If you remove the “GameObject” before “go = …”, then it will assign it to the class variable named “go”, which is probably what you intended.
Quick follow up if you don’t mind:
Isn’t this kind of Object Pooling what I’ve done here?
I mean, is there any advantage in this case using the advanced stuff (from Github or YT) with the singleton etc.?
Not even remotely close, I can’t even figure out what you think Object Pooling is to be a starting for explaining how this is not that.
There are many advantages to many different techniques of doing things, but to explain that I’d need to know what your goals are and what specific techniques you’d like to discuss.
I was talking about the general idea to instantiate an object only one time at start and only set it active when needed instead of instantiate it every time.
So my usecase is, I want to spawn a particle effect each time the gameobject collides.
Do I have for this specific usecase any advantage using object pooling instead of the setactive(true) variation?
Oh, okay. I guess that’s kindasorta like object pooling, except that “pool” generally implies there being more than one of the thing.
If there’s any chance that more than one of these effects can happen at once, I’d recommend using actual object pooling, with support for multiple objects, and there are many tutorials out there for these systems. Otherwise one effect will vanish after you collide with a second thing, and your game will look amateurish.
Most object pooling systems are easily adapted to multiple types of objects/multiple prefabs, so once you have it set up for this collision effect, you can use the same pooling system for enemies, powerups, collectibles, other effects, etc.
Making this object a singleton would be helpful in that your object pool will be accessible to any of your code anywhere, which is often very handy.