- Home /
An object reference is required for the non-static property, method, or property 'UnityEngine.Component.transform.get' and 'UnityEngine.Component.gameObject.get'
Good evening everyone. I have a code here for a 2d platformer game.
using UnityEngine;
using System.Collections;
public class Coin : MonoBehaviour {
public static Vector3 offset, rotationVelocity;
public static float recycleOffset, spawnChance;
// Use this for initialization
public static void SpawnIfAvailable(Vector3 position){
if(spawnChance <= Random.Range(0f, 100f)) {
return;
}
transform.localPosition = position + offset;
gameObject.SetActive(true);
}
void Start () {
}
// Update is called once per frame
void Update () {
if(transform.localPosition.x + recycleOffset < Running.distanceTraveled){
gameObject.SetActive(false);
return;
}
transform.Rotate(rotationVelocity * Time.deltaTime);
}
void OnTriggerEnter () {
Running.AddBoost();
gameObject.SetActive(false);
}
}
now, I'm having a problem. It says: "An object reference is required for the non-static property, method, or property 'UnityEngine.Component.transform.get' " and "An object reference is required for the non-static property, method, or property 'UnityEngine.Component.gameObject.get'". Can someone help me fix it? thanks
probably something about trying to access a gameObject instance in a static method.
Answer by rutter · Mar 12, 2014 at 06:54 PM
Your problem is here:
public static void SpawnIfAvailable(Vector3 position){
if(spawnChance <= Random.Range(0f, 100f)) {
return;
}
transform.localPosition = position + offset;
gameObject.SetActive(true);
}
SpawnIfAvailable
is a static function, which means that transform
and gameObject
aren't available.
Instance fields only exist as part of an object. Your static function is associated with the class as a whole, not with any particular object of that class. Static code can only access statically available data.
You don't show any code that calls SpawnIfAvailable
. Does that need to be a static function? If you're calling it on a particular component, go ahead and make it an instance function. If you do need it available statically, you might consider a singleton pattern.
using UnityEngine;
using System.Collections.Generic;
public class Platform$$anonymous$$anager : $$anonymous$$onoBehaviour {
public Coin coin;
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
public Vector3 $$anonymous$$Size, maxSize, $$anonymous$$Gap, maxGap;
public float $$anonymous$$Y, maxY;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
objectQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i < numberOfObjects; i++){
objectQueue.Enqueue((Transform)Instantiate(prefab));
}
nextPosition = startPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
}
void Update () {
if(objectQueue.Peek().localPosition.x + recycleOffset < Running.distanceTraveled){
Recycle();
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range($$anonymous$$Size.x, maxSize.x),
Random.Range($$anonymous$$Size.y, maxSize.y),
Random.Range($$anonymous$$Size.z, maxSize.z));
Vector3 position = nextPosition;
position.x += scale.x * 0.5f;
position.y += scale.y * 0.5f;
position.z = 0;
//Coin.SpawnIfAvailable (position);
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range($$anonymous$$Gap.x, maxGap.x) + scale.x,
Random.Range($$anonymous$$Gap.y, maxGap.y),
Random.Range($$anonymous$$Gap.z, maxGap.z));
if(nextPosition.y < $$anonymous$$Y){
nextPosition.y = $$anonymous$$Y + maxGap.y;
}
else if(nextPosition.y > maxY){
nextPosition.y = maxY - maxGap.y;
}
}
}
this is the code that uses the spawnIfAvailable. Removing the word static will give me an error that an object reference is required for the non-static field, method, or property coin.spawnIfAvailable(UnityEngine.Vector3)
That's because you were calling it on Coin with a capital C. Coin
is a type coin
is an instance of that type. You probably meant to use coin
ins$$anonymous$$d
thanks. It removed the error but during the game, I get this error:
NullReferenceException: Object reference not set to an instance of an object Platform$$anonymous$$anager.Recycle () (at Assets/Platform/Platform$$anonymous$$anager.cs:43)
Well yeah, because in the code you posted, you never initialize coin. You either need to instantiate a new one somewhere, or drag a gameobject that has a coin component to that variable in the editor.
"drag a gameobject that has a coin component to that variable in the editor."
Your answer

Follow this Question
Related Questions
use list of gameobjects transform in raycast 0 Answers
Transform values not exact 2 Answers
Moving a GameObject to a certain point in world space via script 1 Answer
Camera rotation the same as player rotation 1 Answer
Creating an array of prefabs? 4 Answers