Author Topic: GameObject.Find don't work after a new level load  (Read 2434 times)

synoptik

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
GameObject.Find don't work after a new level load
« on: July 24, 2013, 12:41:05 PM »
Well, the title pretty much explains them problem itself.  I'm using GameObject.Find to find certain gameobjects that are instanciate by TNManager.Create in the AutoCreate script.  When I change the level (or restart the loaded one) I can't find the gameobject anymore even if the name of the gameobject never change.  What's weird is that I still see the gameobject in the hierarchy when I start the server in the editor and changing level.

Is there something I'm missing ? (most likely yes)

Before someone ask, I'm using GameObject.Find to set the target of the Smooth Follow camera script attach to the instructor camera (who need to select wich student he is following).  For memories, I'm making a driving simulator for educationnal purpose for emergency vehicules (ambulance and police).

Or if there's another, most effective way to find the gameobjects (their names never change since they are the simulator name : Sim1, Sim2... ) I'm open to suggestions !

EDIT : typos

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: GameObject.Find don't work after a new level load
« Reply #1 on: July 24, 2013, 02:13:26 PM »
1. Never use GameObject.Find. It's SUPER slow, and referencing something by name is generally a terrible idea. Instead create a singleton script. TNManager is a singleton, for example.

2. When you switch levels, all instantiated objects get destroyed, so why would it still be there? Even if it was somehow still there, GameObject.Find will not find deactivated objects, and depending on when you try to do that (in Awake or OnEnable for example) -- it simply will fail.