Accessing C# Script from a other C# Skript

Hello,
well i try to change a parameter in a C# Script that is attached to a GameObject via a MouseDown.
If i do it with javascript it works but then i can only access a other javascript parameter. But all is in C# and that’s where i run into problems… :cry:

Here is the Car.cs Script and i want to change accel via a other C# script.

	void FixedUpdate () {
		
		
		float accel = 0; // accelerating -1.0 .. 1.0
		bool brake = false; // braking (true is brake)
				
		if ((checkForActive == null) || checkForActive.active) {
			
		steer = iPhoneInput.acceleration.y);
	accel = (-iPhoneInput.acceleration.z);

and her is my MouseDown Script:

public class MouseDown : MonoBehaviour {

void FixedUpdate () {

		
		if (Input.GetMouseButtonDown(0)) 
		{ 


	Car.accel = 1.0f;
	
		//Application.OpenURL ("http://unity3d.com/");
		}
	}
}

If i use “OpenUrl” all works fine.

Maybe someone can give me a example. I have search the Forums and read all the Java Scrips and if i do a Test with .js it work but with C# it doesn’t.
many thxx

Variables declared inside a function are local to that function and can’t be accessed externally. Your accel variable needs to go outside of the FixedUpdate function but inside the class body, and it also needs to be marked as public to make it accessible for other classes:

public class Car : MonoBehaviour
{
	public float accel = 0.0f;

	void FixedUpdate()
	{
		...
	}
}

Elsewhere, your other scripts should use GetComponent to get a reference to the script in essentially the same way as you would in Javascript (give or take a few casts):

Car car = (Car)theObjectWithTheCarScript.GetComponent(typeof(Car));
car.accel = 1.0f;

You could also make the accel variable static (write it just after the public keyword) to make it possible to use Car.accel without having to use GetComponent first, but this is generally a bad idea as it would make it difficult to, for example, have more than one car.

Hi,
well many thxx i got it ! :smile: