I have problems to access functions and variables of scripts which are attached to other objects
From the manual I read
Accessing Other Game Objects
function Update () {
var otherScript: OtherScript = GetComponent(OtherScript);
otherScript.DoSomething();
}
I suppose that the function Update() belongs to a script which is attached ,lets say, to Object_A while "OtherScript " is a script which is attached ,lets say,to Object_B
I get the the error msg :
"the name “OtherScript” does not denote a valid type (‘not found’) "
Obviously the script “OtherScript” does exist,this script being attached to Object_B
if I write
var otherScript: = GetComponent(OtherScript);
I dont get any error msg
if I print(otherScript) it returns ‘OtherScript’
but
print(otherScript.foo) return ‘0’ while I set foo = 5 in OtherScript
var otherScript : OtherScript;
function Start ()
{
otherScript = GetComponent(OtherScript);
//the o in other in theis line of code was a large O not a small o.
otherScript.DoSomething();
}
Make sure you use correct gamma in coding . i don’t know any JavaScript so no hate
You either have to manually drag the gameobject over in the inspector, Find the gameobject by a tag, use collision to give you the reference, raycast to get the other gameobject to reference…
Their are many ways to get the other gameobject and you will have to use one of them. The easiest being you manually dragging the game object on the script your wanting to reference.
It is exactly what I did but it does not work,maybe I am doing something wrong
Anyway you find the game object to which the script you want to access is attached
In your example “someObject” but it is not what it is written in the manual
According to the manuak you should simply write
var theSkript : TheSkript = GetComponent(TheSkript);
(see my first post)
The only way that might not work is if you’re mixing languages. It’s best not to in this case, but if it’s absolutely necessary, then see the section in the docs about script compilation order.
The prefab script “SmoothFollow” is attached to “MainCamera” , my script “Shooting” is attached to my prefab “Bullet”
Both are JavaScripts
I want to access my boolean variable “hit” of “Shooting” from “SmoothFollow” in order to change the initial “Transform” of the variable “target” of “SmoothFollow”
If hit == true, MainCamera should start following an other object
Is it possible ?
Maybe not the most efficient way but this is the easiest way I could think of to do what you want to do. First give the game object that you want to access a tag. Then find that game object by using GameObject.FindGameObjectsWithTag. Next you can access its components with GetComponent, where the component is the name of the script you want to access.
public var target : GameObject[];
function Start()
{
target = GameObject.FindGameObjectsWithTag("TagName");
target[0].GetComponent<SmoothFollow>().hit = true;
}
function Update()
{
if (target[0].GetComponent<SmoothFollow>().hit == true)
{
//code here
}
}