- Home /
The question is answered, right answer was accepted
Script is changing default values
When I do it this way
for(var i : int = 0;i < (possibles.Length);i++)
{
possibleScripts[i] = possibles[i].gameObject.GetComponent("GunScript");
possibleScripts[i].cashPrice = possiblesCosts[i];
(((possibleScripts[i].shotMaxDamage + possibleScripts[i].shotMinDamage)/2) * (possibleScripts[i].ammo )* (2.65 ))+ (PlayerScores.difficulty * 12);
}
isntead of
doScript = currentGun.gameObject.GetComponent("GunScript");
doScript.cashPrice = possiblesCosts[gunToPick];
it changes the values of the prefab associated, instead of just the instance. Is this because the guns are added into an array beforeHand, and are thus just referencing the prefabs? If so is there a way I can pre spawn them?
I mean, this works, but it seems "Less correct".
Answer by Tourist · Oct 06, 2014 at 07:05 AM
It is because the guns are not instantiated and you just modifying the prefabs.
You may instantiate the game objects and deactivate them so they are in the hierarchy but act like they do not exist or you can keep those values in a structure always present and when you instantiate a gun from its prefab you give it the data structure for it.
Answer by LSPressWorks · Oct 06, 2014 at 02:16 PM
I figured as much. I will code in that direction first I guess. Test it out and see how it behaves compared to the current functioning method. Thanks Tourist.
Follow this Question
Related Questions
How to declare and access other scripts publicly in Csharpe? 3 Answers
Scripts won't load 0 Answers
What's the Script for Combining Prefabs 2 Answers
Adding score to GUI Text via another script/variable. 3 Answers
Error car game 0 Answers