Do I have to tell unity to load assets only once with cSharp script

I have this cSharp code which runs fine, but I want to add a boolean run_once control to my code as it is part of a class that scans input and is called by Update

		Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
		GameObject playerMissile001 = initializeModels.playerMissile001;
		GameObject playerMissile002 = initializeModels.playerMissile002;
		GameObject currentMissile = (GameObject)playerMissile001;

If I enclose the code with a boolean run_once set to false in a containing class as such :

if(!run_once)
{
       Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
		GameObject playerMissile001 = initializeModels.playerMissile001;
		GameObject playerMissile002 = initializeModels.playerMissile002;
		GameObject currentMissile = (GameObject)playerMissile001;
		run_once = true;
}

I get errors such as :

Assets/Scripts/player001controls.cs(161,42): error CS0135: `playerMissile001’ conflicts with a declaration in a child block

So, how do I get around this, and do I have to? Perhaps unity runs those lines of code only once as they refer to loading assets.

HI, can’t really see what’s the problem with only that little part of your code but “conflicts with a declaration in a child block” means you have another declaration of “playerMissile001” in another block when you should have only one. Use CTRL+F to find where you’re declaring the objects and remove those not needed.

For what it’s worth, those objects you are declaring inside the if { } are going to vanish immediately after you exit the code block. I have a feeling you mean to do this:

GameObject playerMissile001 = null;

if (!run_once) {
    playerMissile001 = initializeModels.playerMissile001;
    run_once = true;
}

Which means you already declared it before... Try to remove "GameObject" before the local variables in the "if" block like :

`
    if(!runonce)
    {
           Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
           playerMissile001 = initializeModels.playerMissile001;
           playerMissile002 = initializeModels.playerMissile002;
           currentMissile = (GameObject)playerMissile001;
           runonce = true;
    }
    
`

No, I can use static variables instead.

With static variables I don’t need to create an instance of InitializeModels.
Because of this I removed this line of code that instantiated InitializeModels :

InitializeModels initializeModels = new InitializeModels(); 

I added the word static after public or private to all the variables inside the class InitializeModels()

I removed this code :

 if(!run_once) // Removed this code block
 {
   Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
   GameObject playerMissile001 = initializeModels.playerMissile001;
   GameObject playerMissile002 = initializeModels.playerMissile002;
   GameObject currentMissile = (GameObject)playerMissile001;
   run_once = true;
}

I removed this code declaring run_once as it isn’t called anymore :

	bool run_once = false;

I replaced all occurences of initializeModels. with InitializeModels