Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by egElskarNoreg · Mar 12, 2014 at 06:07 PM · gameobjecttransformobjective-cnonstatic

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · Mar 12, 2014 at 06:09 PM 1
Share

probably something about trying to access a gameObject instance in a static method.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image egElskarNoreg · Mar 12, 2014 at 07:25 PM 0
Share
 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)

avatar image perchik · Mar 12, 2014 at 07:26 PM 0
Share

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

avatar image egElskarNoreg · Mar 12, 2014 at 07:42 PM 0
Share

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)

avatar image perchik · Mar 12, 2014 at 07:44 PM 0
Share

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.

avatar image perchik · Mar 12, 2014 at 07:56 PM 1
Share

"drag a gameobject that has a coin component to that variable in the editor."

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges