GetComponent (493734)

Hello

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

What am I doing wrong?

gameObject.GetComponent(OtherScript);

I tried also this but it does not seem to work ,either
In any case the manual should be fixed since it is not what it says
I

try:

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 :stuck_out_tongue: . i don’t know any JavaScript so no hate :slight_smile:

are you sure the script you want to access is attached to the same gameObject? If not, you will have to use Find to get to nthat gameObject

the script I want tp access is not attached to the same GameObject , that’s why I referred to the section of the manual : Accessing Other Game Objects

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.

than you have to find the game object first

example:

var someObject : GameObject; //Through inspector assignable reference
var theSkript : TheSkript = someObject.GetComponent(TheSkript);

you can also use alot of other methodes to find the gameObject here

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.

–Eric

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
  }
}

You could also go about this in reverse. Getting a reference to your main camera is easier.

Shooting script:

if(hit)
{
      Camera.main.GetComponent(SmoothFollow).target = someTransform;
}

Tanel
Thanks it works
The simpler, the better