- Home /
Automatically uncheck?
hi, i have a problem with gameobject again. i attach the script in a gameobject, and then i Clone that gameobject, then destroy the original gameobject. my question is Why the script automatically uncheck in cloned gameObject ? and then how can i fix it?? thanks
Ugh...i posted an answer to this like 3 hrs ago...I wish my account didnt get screwed up in the move to dev unity wtevr site.
boolean defaults to false..so if you create a new instance of an object that has the same script(unless declared otherwise) it will start out false (regardless of inspector-since the inspecter reference was to the parent and not this clone).
To fix
Simply say that this variable = true upon instantiation
public var myPlayer:Transform;
function ClonePlayer()
{
var clone=Instantiate(myPlayer,myPlayer.position,myPlayer.rotation);
clone.GetComponent($$anonymous$$yScript).variable=true;
}
please dont comment as the answer, so i get notification about a new answer. But, hey thanks OperationDogBird, your answer its true, now my gameobject work properly. Thank you very much
Answer by OperationDogBird · Jul 09, 2012 at 09:55 AM
Boolean defaults to false (unchecked) when created. If you dont make that specific boolean true (check) when it is created it assumes the default value of false. You can easily remidy the problem by simply saying something of the sorts
var playerClone=Instantiate(player,position,rotation);
playerClone.GetComponent(MyScript).myVar=true;
notice the variable for the newly instantiated object and then the ref to the actual script that we need to change to get the desired results.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
static var for one object. 2 Answers
How to Destroy a gameobject on collision 3 Answers
Destroy on Collision? 1 Answer
Instantiate problem 1 Answer