- Home /
how to know which script you change?
How do I know I changed the right script.
I have 4 objects which all have an script called "Item" attached.
function OnCollisionEnter(collision : Collision) { kind = collision.gameObject; }
function LatenVallen(){ if(kind != null) { var fixJoint = kind.GetComponent(FixedJoint); Destroy(fixJoint); kind.gameObject.rigidbody.useGravity = true; grens = false; script = kind.GetComponent(Item); script.valtaan = true; kind = null; }
How can I change the boolean of Item1 and then use the boolean in Item1. Currently, after I changed the boolean of Item1, Item3 uses it to activated a function which changes the boolean back to false.
I justed named them Item1, Item2, Item3 and Item4 instead of 4x Item.
This ultimately depends on the GameObject stored in kind
. Are all GameObjects to which Item
is attached instants of the same prefab? How are you selecting the one whose Item-script you want to access the boolean in?
the GameObjects are duplicates of eachother, but I gave them the Item.js after i made them. No prefabs used. Used the OncollisionEnter to decide which gameobject to use
the problem is that I don't want to specify which Item to take. I have a character who can grab and drop. As soon as he drops something. the boolean of the item which is dropped must become true.
Answer by CHPedersen · May 10, 2011 at 01:05 PM
If you're not using prefabs but have manually created 4 objects in the editor and then given them the same script, then you can identify them by their name, assuming you've also given them different names.
The Collision-object passed as an argument in OnCollisionEnter contains information on which GameObject you've collided with. Then you can check the name of the GameObjects involved in the collision to see which GameObjects and consequently, which versions of Item
you're accessing booleans in.
Edit: You're kind of doing that already. If you have
function OnCollisionEnter(collision : Collision) {
kind = collision.gameObject;
if(kind.name == "[Whatever name you've given the first GameObject]")
// If true, you're accessing the first version of Item
}
Answer by Jean-Fabre · May 10, 2011 at 11:22 AM
Hi,
If you have more than one component, use GetComponents ( which returns a list of components) and then in each component you have to maintain a variable to differentiate them somehow and check against that. Else call the method in each.
Bye,
Jean
I only have one component called "Item" but I have multiple times, the same script attached to different objects.
Your answer
Follow this Question
Related Questions
Take boolean from other script error 1 Answer
Can someone translate C# to Javascript? 2 Answers
How to make a N-S magnet Javascript? 1 Answer
How do I make a sword swing? 2 Answers
Compiler error UCE0001: ';' expected 1 Answer