Hello. I want change a variable inside a script from other script, the code is: Code (csharp): Laser1 ns = gameObject.GetComponent<Laser1>(); ns.ShootDirection = EnemyPosition; I follow the example in the help, but the code returns me this error: NullReferenceException: Object reference not set to an instance of an object. The console tells me that error is in: Code (csharp): Laser1 ns = gameObject.GetComponent<Laser1>(); The variables are public, where is the problem?
The "Laser1.cs" is in a gameObject, but I want change a vector3 in "Laser1.cs" from other script (with name "AI_Turret.cs"). The Vector3 in Laser1.cs is: Code (csharp): public Vector3 ShootDirection = Vector3.zero; The part of code in the AI_Turret.cs is: Code (csharp): Laser1 ns = gameObject.GetComponent<Laser1>(); ns.ShootDirection = EnemyPosition; Do I have to do anything else? I don't know where is the problem.
if it is in a gameobject is not of interest. GetComponent will only get components on the CURRENT game object if you want it from another game object you must use FindObject(s)OfType
I solved the problem. The problem was with Visual Studio, the synchronization between Unity and Visual Studio was broken. But thanks for all!