Hi all Unity3D scripters i am having a hard time getting a string for a C# script called guimaker.cs
my code and error is bellow, can someone please help
using UnityEngine;
using System.Collections;
public class Text : MonoBehaviour {
public guimaker guiedit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
guimaker guiedit = GetComponent<guimaker>();
}
void OnGUI()
{
GUI.TextField(new Rect(10, 10, 40, 20), guiedit.myString);
}
}
error CS0309: The type `guimaker' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.Component.GetComponent<T>()'
It sounds like guimaker is not a class defined as a component for some reason. I’d make sure the guimaker class has exactly that name (capital letter included), and that it’s properly defined as a class. Is it inheriting from monobehaviour?
Also, if your script does need it, I would use [RequireComponent(typeof(guimaker))].
First, you definitely don’t want to grab a reference to a script component every frame, so you have
to move the reference assignment to another method like Start() or Awake() or something.
Second, you are re-defining the variable in that statement that is in the update:
guimaker guiedit = GetComponent();
You already define the variable at the top of your script here:
public guimaker guiedit;
so, in the Start() or Awake() methods, you would simply initialize the variable like this:
guiedit = GetComponent();
However, you must have a game object to reference from to use the GetComponent method.
So, you will need to make a reference to the object like this:
guiedit = gameObject.GetComponent();
gameObject being whatever object has the guimaker script attached to it.
if your guimaker script is attached to an object named something like GuiMaker, then you can get the reference like this: