I have this code:
// Start is called before the first frame update
public float speed = 17f;
public float speed2direction = 10f;
public Script script;
void Start()
{
}
// Update is called once per frame
void Update()
{
if(transform.position.x >= 0)
{
transform.position += Vector3.down * speed2direction * Time.deltaTime;
transform.rotation = Quaternion.Euler(0, 0, 180);
// if(script.Car_number > 6)
// transform.rotation = Quaternion.Euler(0, 0, 90);
}
else
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if(transform.position.y < -100)
{
DestroyImmediate(gameObject);
}
}
I m trying to acces a script, but i m getting this error: The type or namespace name ‘Script’ could not be found (are you missing a using directive or an assembly reference?)
5 Answers
5
Hi,
I’m assuming your script is called Script.cs
Insert the following:
void Awake()
{
script = GetComponent<Script>();
}
and make sure you have added Script as a component to your game object.
If the script is located on the same gameobject as the script you`re showing.
You could do it like this.
private void OnValidate()
{
script ??= GetComponent<Script>();
}
This will set the script when you go back into the editor.
Great way to remember to set the script.
Hope this helps you!
These are great solutin, but they didn t work for me. I have a script called " Spawner.cs " and it s an assingned to an Emty Object called Spawner_GameObject. I want to get a variable from this script in other script called " Enemy_Movement".
Given the new information:
When you have a Script called “Spawner” then it must contain a class “Spawner” that inherits from MonoBehaviour:
public class Spawner : MonoBehaviour
Assuming that you have this - this now defines a Type. The Type is Spawner.
When you define a variable in which you want to reference a script then the Type of that variable must be correct.
So in your case you want to write:
//taken from your code in your first post:
public float speed = 17f;
public float speed2direction = 10f;
public Spawner spawner; // reference slot for a spawner.
void Start()
{
}
// Update is called once per frame
void Update()
{
....
Notice that the variable is now of Type Spawner instead of Script.
At the point when you characterize a variable where you need to reference a content then the Kind of that variable should be right.
So for your situation you need to compose
what’s more, ensure you have added Content as a part to your game item. Pinterest MOD
what is the class name of the script you are trying to access?
– Captain_PineappleThese are great solutin, but they didn t work for me. I have a script called " Spawner.cs " and it s an assingned to an Emty Object called Spawner_GameObject. I want to get a variable from this script in other script called " Enemy_Movement".
– unity_46QOvONZ1Xy6sA