- Home /
Error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Object.name' (CS0236)
Cant figure out what is wrong. Need help making the array into different ints. The array uses the game object name. an example of the name would be 1, 23 or 12, 43 or 23, 0. It is in a prefab which is cloned. The array decodes the position of the game object (in vector 2) and then displays the x position and the y position in the grid. The top part just makes a popup GUI which for now displays the chords.
my code is as fallows
using UnityEngine;
using System;
using System.Collections;
using System.Text.RegularExpressions;
public class targetTile : MonoBehaviour {
public bool isClicked = false;
public string title = "Some name";
public static string value = GameObject.name;
public void Start()
{
}
public void OnMouseDown()
{
isClicked = true;
}
public void OnMouseUp()
{
isClicked = false;
}
public void OnGUI()
{
if(isClicked)
GUI.Label(new Rect(5,5,400,100), "This is " + this.name);
}
// Update is called once per frame
void Update () {
}
static void main()
{
Char delimiter = 's';
String[] substrings = value.Split(delimiter);
foreach (var substring in substrings)
Console.WriteLine(substring);
}
}
feel free to ask question if you need any specifications. the error is on line 12.
Answer by SohailBukhari · Jun 15, 2017 at 05:37 AM
initialize in constructor public static string value; GameObject is a type not static instance which you get directly, or initialize in start or Awake.
public DestoryUpperCase()
{
value = gameObject.name;
}
and in the static method you can't access the non-static members so value needs to be static public static string value;
static void main()
{
Char delimiter = 's';
String[] substrings = value.Split(delimiter);
foreach (var substring in substrings)
Console.WriteLine(substring);
}
@SohailBukhari I put it in the code and now it's co$$anonymous$$g up with "Error CS1520: Class, struct, or interface method must have a return type"
so how would I implement the code??
Paste the code and then check
public bool isClicked = false;
public string title = "Some name";
public static string value;
public void Start()
{
value = gameObject.name;
}
public void On$$anonymous$$ouseDown()
{
isClicked = true;
}
public void On$$anonymous$$ouseUp()
{
isClicked = false;
}
public void OnGUI()
{
if (isClicked)
GUI.Label(new Rect(5, 5, 400, 100), "This is " + this.name);
}
static void main()
{
Char delimiter = 's';
String[] substrings = value.Split(delimiter);
foreach (var substring in substrings)
Console.WriteLine(substring);
}
DestoryUpperCase was just a example in your case your constructor will be
public targetTile ()
{
}
Answer by henry_jvx · Jun 15, 2017 at 05:46 AM
public static string value = GameObject.name;
GameObject is a type, not an instance. the right way is:
public static string value = gameObject.name;
Your answer
Follow this Question
Related Questions
Problem with raycast detection? 1 Answer
How to Shuffle Prefab Clones on a Grid 0 Answers
Click to move in a 3d-blockbased single mesh 1 Answer
Keyboard not working in build (is working in editor) 0 Answers
How can I slow down cell selection 0 Answers