How can change variable of script in child.

i have 2 gameObject. (1)Gameobj1 (2)Gameobj2

childName -childobj1 , childobj2

Gameobj1<->childobj1(scriptName called enemy-> enemy.i=5)

Gameobj2->childobj2(scriptName called enemy-> enemy.i=6)

both childobj have same script ,now i want to change enemy.i

in childobj2 script , but i am unable to do can u let me how i change? thanks in advance

So you have the object via collision or other, you simply need to use

// obj is the reference to your object, it may be named differently
ScriptName script = obj.GetComponentInChildren<ScriptName>();
script.variable = value;

You can use transform.parent to find the parent element of both. Then use GameObject.GetComponentsInChildren to find all the gameobjects. Iterate them, find the one you want and change it’s variable.

Other option is to tag both (left in the inspector) and use GameObject.FindGameObjectsWithTag to find the appropriate gameobject. use GetComponent to get the enemy script and change it’s i value.