Search Unity

Instantiated GameObject in Awake() - not found in Method

Discussion in 'Scripting' started by Doomchecker, Oct 13, 2021.

  1. Doomchecker

    Doomchecker

    Joined:
    Apr 5, 2021
    Posts:
    109
    Hello,

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

    Thanks for any ideas.

    Code (CSharp):
    1. void Awake()
    2. {
    3. GameObject go = (GameObject)Instantiate(publicGo.gameObject);
    4. }
    5.  
    6. void Update()
    7. {
    8. Debug.Log(go);
    9. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    Bunny83 likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
    Doomchecker likes this.
  4. Doomchecker

    Doomchecker

    Joined:
    Apr 5, 2021
    Posts:
    109
    This was exactly the problem, thank you!

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

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  6. Doomchecker

    Doomchecker

    Joined:
    Apr 5, 2021
    Posts:
    109
    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?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
    Doomchecker likes this.