- Home /
GetComponent not working in C#
I am trying to reference another script for a bool called move. Here is script #1
using UnityEngine; using System.Collections;
public class Crate_Forward : MonoBehaviour {
public float moveSpeed = 10f;
public bool move;
// Use this for initialization
void Start () {
move = false;
}
// Update is called once per frame
void Update () {
if (move == true) {
if (Input.GetKey (KeyCode.Keypad8)) {
transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);
}
}
}
}
In Script #2 i put this to reference the bool move: //the object script #1 is located in is called crate
if (GameObject.Find("crate").GetComponent().move)
This does not seem to work as Unity Editor does not recognize the script Crate_Forward.
*These scripts are located in different objects
Answer by TwinPrime · May 24, 2015 at 03:00 PM
in script num 2 add this public variable ;
public Crate_Forward script;
then grap and drag your scipt in it. then in start func;
script = gameObject.GetComponent<Crate_Forward>;
then in your update func;
script.move();
Hope it helps
Thanks but i still get this error in the console message.
Assets/Standard Assets/Characters/RollerBall/Scripts/Ball.cs(28,24): error CS0246: The type or namespace name `Crate_Forward' could not be found. Are you missing a using directive or an assembly reference?
When i typed public Crate_Forward script; It made me insert a gameObject not a script.
Answer by DiegoSLTS · May 24, 2015 at 03:27 PM
GetComponent works in C#, the problem in your code is somewhere else. I just copied your "Crate_Forwad" class code and setup an object called "crate" and I can write this line in another script without any compiler error:
Crate_Forward script = GameObject.Find("crate").GetComponent<Crate_Forward> ();
Make sure your class "Crate_Forward" is defined inside a file named "Crate_Forward.cs", the file name and the class inside it must always match.
Also, make sure there are no other compiler errors in your code.
It still says Crate_Forward cannot be found in the current text. Both the class and script name are the same as well. "Crate_Forward"
here is the error
Assets/Standard Assets/Characters/RollerBall/Scripts/Ball.cs(29,17): error CS0246: The type or namespace name `Crate_Forward' could not be found. Are you missing a using directive or an assembly reference?
Just to be clear, when you select the Crate_Forward file in your project window inside Unity it displays "Crate_Forward.cs" at the bottom?
It's too weird that it's not working. As I said, I just copy pasted your code and Unity recognices the class. $$anonymous$$aybe you have other scripts conflicting with your Crate_Forward script, I see you have your Ball asset inside the Standard Assets subfolder, so maybe there.
Or maybe your file has some invalid characters on it, did you copy past code from somewhere? Try coping your Crate_Forward code in some temporary place (like a text file outside the project) and recreate the file in Unity, writing the code manually.
Also,can you share a screenshot of your Project window in Unity?
here are screenshots of the two file locations. I tried recreating the script manually as well but it had the same outcome.
Here are the locations of the two files. I also tried to manually recreate the "Forward_Crate.cs" file but it still did not work.
If you comment the line in Ball.cs giving the error, the code compiles? Everything else works?