- Home /
error CS0118: `New_Career.Fame' is a `field' but a `type' was expected
Hello
Excuse me if there is a simple answer to this, for I am not experienced with C# and am more comfortable with Javascript but for some reason I keep getting the error error CS0118: New_Career.Fame' is a
field' but a `type' was expected. I have had problems with this piece of code before but I cannot find an answer on the web or Unity Answers. I am trying to access a public variable from my other script "Career_Variables"
Here is my code
using UnityEngine;
using System.Collections;
public class New_Career : MonoBehaviour {
int Fame = GameObject.Find("NewCareerText").GetComponent<Fame>();
int Fame_level = GameObject.Find("NewCareerText").GetComponent<Fame_level>();
int money = GameObject.Find("NewCareerText").GetComponent<money>();
int Races_Completed = GameObject.Find("NewCareerText").GetComponent<Races_Completed>();
int Races_won = GameObject.Find("NewCareerText").GetComponent<Races_won>();
int Races_lost = GameObject.Find("NewCareerText").GetComponent<Races_lost>();
// Use this for initialization
void OnMouseEnter ()
{
gameObject.renderer.material.color = new Color(0,0,0);
}
// Update is called once per frame
void OnMouseExit()
{
gameObject.renderer.material.color = new Color(255,255,255);
}
void OnMouseDown()
{
Application.LoadLevel ("Career");
}
}
Thank you in advance for your help and if there are any details I have left out please tell me
Answer by perchik · Apr 08, 2014 at 08:45 PM
Right,
int Fame = GameObject.Find("NewCareerText").GetComponent<Fame>();
says to create an integer named "Fame" and set its value to (the compiler finds a gameobject called NewCareerText and gets the Fame script component off of it). This will return an error because GetComponent
returns an object of type Fame, not type int
My guess is that you have some other script which has a integer fame variable that you're trying to get in which case you'd want to do something like
OtherScript other = GameObject.Find("NewCareerText").GetComponent<OtherScript>();
int fame = other.fame;