- Home /
How can i make GetComponent(SCRIPT) apply to ONLY ONE object that has the script?
Ive looked and looked. Tried and tried. I dont understand why this wont work.
Say you have 100 objects with the same script.
Each of those Objects has children.
If i mouse click on a child, i want that child to:
1:Identify its parent
2:Change a variable of the parent's script
3.Then destroy itself
The problem is when i click on the child, it changes all 100 object's variables that have the same script. I am using:
target = transform.parent.GetComponent(ParentScript);
target.BooleanVar = true;
target.IntegerVar = 1;
Thanks abunches! :)
This is not enough information. How do you detect your "clicks"? Please add all relevant parts of your script to the question. At the moment the question can't be answered.
Feel free to edit your question
go to unitygems.com for a full explanation and tutorial of GetComponent
Answer by hvilela · Oct 17, 2012 at 11:02 PM
I can see two possible causes:
Your variables at "ParentScript" are static, meaning that they belong to the class, not to the instance.
You have an error in your click detection function and your script is changing it all.
Without your click detection and your ParentScript codes, it's impossible to tell.
YES!! $$anonymous$$y variables in my ParentScript are static. So should I make them public??? Thanks so much for helping me. (Even with the little information I provided ha )
Yes, remove the static keyword. Static means it belongs to the class, or in other works, that you have just one "BooleanVar" and one "IntegerVar" shared between all the script instances.
Your answer
Follow this Question
Related Questions
Talking To Other Scripts 1 Answer
Make a simple tree 1 Answer
Accessing parent gameobject script not working 1 Answer
Get variable from another object 1 Answer
Putting a variable of a child in the inspector for the parent 1 Answer