- Home /
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.
Answer by maggot · Dec 24, 2011 at 04:39 PM
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
Answer by ks13 · Nov 25, 2011 at 01:34 PM
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.
I think the problem has to do with scope limits. Perhaps if I enclose the code in an if statement, it becomes inaccessible outside the If statement.
Answer by ptdnet · Nov 25, 2011 at 01:56 PM
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;
}
O$$anonymous$$, that's working, although I had to make a few changes to my code. I added all relevant GameObject types to my initialize$$anonymous$$odels class with get{} and set{}, assigned all GameObject types to null in the declaration part of Player001Controls, and edited one line of code which gave an error.
Answer by ks13 · Nov 25, 2011 at 04:02 PM
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;
}
Well I got it working with those GameObject type declarations in the If block before, but I removed them now and it still works fine.
Your answer
Follow this Question
Related Questions
Generating and scrolling assets 0 Answers
C# PlayerPrefs not Work 2 Answers
how to preload assets with progress bar 1 Answer
LoadAssetAtPath() returning null IN EDITOR 1 Answer
loading prefabs 1 Answer