- Home /
How can I manipulate the script attached to a Transform Object?
I have been using a script that controls elements in the scene as Transform objects. Those objects have a javascript,and are within a GameObject.
I have found some trouble when I want to access their script, because what I have found, only works for GameObjects, not for Transform.
can you give me an idea howo to solve this? in code, or reoganizing my code
thanks
I have something like this
player script:
var health = int;
health=100;
enemy script:
var target : Transform; // I place my player here in the editor
var playerHealth= GetComponent(CameraControlPlayer).health;
if(playerHealth>10)
atack();
on the enemy script, I am not able to read or manipulate the values or variables from player(Transform) script.
Answer by TheDarkVoid · Jun 21, 2012 at 09:57 PM
u need to do a
public static var health = int;
in order to access the var.
Answer by Berenger · Jun 21, 2012 at 09:59 PM
That line
var playerHealth= GetComponent(CameraControlPlayer).health;
is actually
var playerHealth = this.GetComponent(CameraControlPlayer).health;
Which means it's going to return the component CameraControlPlayer attached to the same gameObject this script is attached to. So, it will return null. Try
var playerHealth = target.GetComponent(CameraControlPlayer).health;
I've try that, but the problem was not there, it was the static public var, that kingk614 commented. But I was hoping for more solutions to get more experience on the error. Thanks! =)
Answer by keitasama · Jun 22, 2012 at 10:08 AM
@kingk614: yes that solved my problem. what I wrote at the end was: public static var health = int; health=100; enemy script:
var target : Transform; // I place my player here in the editor var playerHealth= target.GetComponent(CameraControlPlayer).health; if(playerHealth>10) atack();
Answer by TheDarkVoid · Jun 22, 2012 at 02:36 PM
also if you ever wanted a access more from that script you should do something like this:
//C#
public GameObject ScriptObject;
public ScriptName SomeName;
//in the update()
SomeName = ScriptObject.GetComponent<ScriptName>();
//u can now use the SomeName variable to access any
//public variable or function from ur script.
//Java Script
public ScriptObject : GameObject;
public SomeName : ScriptName;
//in the update()
SomeName = ScriptObject.GetComponent(ScriptName);
//u can now use the SomeName variable to access any public
//variable or function from ur script.
Hope this helps you.
Your answer
Follow this Question
Related Questions
How to destroy a transform's parent object. 1 Answer
Attach Transform to another GameObject 0 Answers
guiText/GameObject position 2 Answers