Hi guys,
i tried to get a variable float static from a script to script but it’s tells me:
error CS0246: The type or namespace name `FirstPersonController’ could not be found. Are you missing a using directive or an assembly reference?
here’s the first script :
public FirstPersonController Script;
public float aimRacio = 0.4f;
void Update () {
Script.currentAimRacio = 1;
}
and the second code :
public static float currentAimRacio = 1;
void Update()
{
m_MouseLook.XSensitivity *= currentAimRacio;
m_MouseLook.YSensitivity *= currentAimRacio;
}
And i’m using Unity 5.1.1
Hello 3B, if your trying to use the Standard Assets FirstPersonController script, you have to include this Using statement up at the top:using UnityStandardAssets.Characters.FirstPerson;
Typically, the type of error your getting pops up only when you are not accessing the correct namespace (by using the correct Using statements at the top of the script), so next time you get the same sort of error, check that your including the proper namespaces. The best way to check this, is by opening the script you wish to access, and checking what its namespace is and copying that namespace into your script and putting a “using” in front of it.
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
// This is the namespace below, just copy everything after the
// "namespace" bit, but before the curly brace and paste it as a
// using statement in your code.
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (AudioSource))]
public class FirstPersonController : MonoBehaviour
{
Still same problem,
i used that namespace it comes with the script
Please help me @TonanBora
Help me, is it from Unity 5 only ?!
Could you post all the using statements that you have in the first script, exactly as you have them?
public FirstPersonController Script;
public float aimRacio = 0.4f;
public void AimDownSights()
{
void Update () {
AimDownSights ();
}
if (Input.GetButton ("Fire2")) {
Script.currentAimRacio = aimRacio;
} else {
racioHipHold = Mathf.SmoothDamp (racioHipHold, 1, ref racioHipHoldV, hipToAimSpeed);
currentFov = Mathf.SmoothDamp(currentFov, 60, ref fovV, SmoothTime);
//Script.currentAimRacio = 1;
}
}
Second Script :
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
using UnityStandardAssets.Characters.FirstPerson;
// This is the namespace below, just copy everything after the
// "namespace" bit, but before the curly brace and paste it as a
// using statement in your code.
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (AudioSource))]
public class FirstPersonController : MonoBehaviour
{
public static float currentAimRacio = 1;
}
Your first script :
public FirstPersonController Script;
public float aimRacio = 0.4f;
public void AimDownSights()
{
void Update () {
AimDownSights ();
}
if (Input.GetButton ("Fire2")) {
Script.currentAimRacio = aimRacio;
} else {
racioHipHold = Mathf.SmoothDamp (racioHipHold, 1, ref racioHipHoldV, hipToAimSpeed);
currentFov = Mathf.SmoothDamp(currentFov, 60, ref fovV, SmoothTime);
//Script.currentAimRacio = 1;
}
}
Is the one that needs the using statement at the top.
So in this script copy and past the following code at the very top.
using UnityStandardAssets.Characters.FirstPerson;
Still same problem here’s the new code :
using UnityStandardAssets.Characters.FirstPerson;
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
// This is the namespace below, just copy everything after the
// "namespace" bit, but before the curly brace and paste it as a
// using statement in your code.
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (AudioSource))]
public class FirstPersonController : MonoBehaviour
{
public static float currentAimRacio = 1;
}
}
Also, another question, why are you trying to use a static variable?
You might want to use a nonstatic float instead, depending on what you plan on using said variable for.
Now, your first script, this one:
public FirstPersonController Script;
public float aimRacio = 0.4f;
public void AimDownSights()
{
void Update () {
AimDownSights ();
}
if (Input.GetButton ("Fire2")) {
Script.currentAimRacio = aimRacio;
} else {
racioHipHold = Mathf.SmoothDamp (racioHipHold, 1, ref racioHipHoldV, hipToAimSpeed);
currentFov = Mathf.SmoothDamp(currentFov, 60, ref fovV, SmoothTime);
//Script.currentAimRacio = 1;
}
}
Has a few flaws in it, besides the whole namespace issue.
For instance, you some how put the Update function inside your AimDownSights method. In addition, it seems that the code for your AimDownSights method is outside the actual method, and the method is only called in the Update function (which in turn, is only called when the AimDownSights method is called).
Below is a cleaned up version of your script:
NOTE: Make sure you replace “YourScript” on line 5 with the name that you gave the script.
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class YourScript: MonoBehaviour {
public FirstPersonController Script;
public float aimRacio = 0.4f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
AimDownSights();
}
public void AimDownSights() {
if (Input.GetButton("Fire2"))
{
Script.currentAimRacio = aimRacio;
}
else
{
racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, ref racioHipHoldV, hipToAimSpeed);
currentFov = Mathf.SmoothDamp(currentFov, 60, ref fovV, SmoothTime);
//Script.currentAimRacio = 1;
}
}
}
it’s works thanks !!, but
Why it’s gives me error in line 24
Which is Script.currentAimRacio = aimRacio;
it’s gives me that’s error, Can u explain guys why is this happening ?!
What error is it giving you?
NullReferenceException: Object reference not set to an instance of an object
You need to assign a FirstPersonController script to the “Script” variable.
Since it is a public variable, you should be able to assign it through the inspector by clicking and dragging the First Person Controller in your scene, into the value field of the Script variable.
You’re getting that error because you’re trying to access something that is not there yet, basically, you’re trying to pull something out of an empty box.
oh i forgot that !
Thank you very much !