- Home /
The question is answered, right answer was accepted
What's wrong in "simple" script?
I have simple problem, but I wasn't found good answer. I have two (in my opinion) good scripts, but they are making errors (variables are in other language, sorry).
using UnityEngine;
using System.Collections;
public class GłówneInfoGeneracji : MonoBehaviour {
public int MaksymalnaIlośćPomieszeń;
public int ObecnaIlośćPomieszczeń;
void Start ()
{
MaksymalnaIlośćPomieszeń=Random.Range (50,100);
}
void Update ()
{
}
}
using UnityEngine;
using System.Collections;
public class GenerowanieLosowe : MonoBehaviour {
GameObject pomieszczenieSpawn=GameObject.Find ("PomieszczenieSpawn");
GłówneInfoGeneracji główneInfoGeneracji=pomieszczenieSpawn.GetComponent<GłówneInfoGeneracji>();
int MaksymalnaIlośćPomieszczeń=główneInfoGeneracji.MaksymalnaIlośćPomieszeń;
int AktualnaIlośćPomieszczeń;
void Start ()
{
print (MaksymalnaIlośćPomieszczeń);
}
void Update ()
{
}
}
Error: Assets/Prefaby/Pomieszczenia/GenerowanieLosowe.cs(8,49): error CS0236: A field initializer cannot reference the nonstatic field, method, or property GenerowanieLosowe.pomieszczenieSpawn' Assets/Prefaby/Pomieszczenia/GenerowanieLosowe.cs(10,40): error CS0236: A field initializer cannot reference the nonstatic field, method, or property
GenerowanieLosowe.g��wneInfoGeneracji'
I know, this issue was raided many times, but I really musn't find errors. I'm sorry for your time and please help.
In the future, google the exact error message and you'll usually get pointed in right direction.
Answer by meat5000 · Jun 24, 2014 at 07:36 PM
Perform your Find and GetComponent within Start() function instead of in the Declarations.