Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Govqtrpetp · Aug 02, 2018 at 01:48 AM · c#error messagefunctionsthiscase

Having issue with an object reference is required to access a non-static member.

Trying to make an object spawn in different cases and now getting an error for an object reference required to access a non-static member.


Here is the first script where the cases are declared:


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Berry.Utils;
 
 // Base class for slots
 public class Slot : MonoBehaviour {
 
     public static Slot Instance;
 
     public static Dictionary<int2, Slot> all = new Dictionary<int2, Slot>();
 
     public bool generator = false;
     public bool teleportTarget = false;
     public Jelly jelly; // Jelly for this slot
     public Jam jam;
     public IBlock block; // Block for this slot
     public bool insideWallT;
     public bool insideWallB;
     public bool insideWallL;
     public bool insideWallR;
 
 
 
     // Position of this slot
     public int2 coord = new int2();
     public int x { get { return coord.x;} }
     public int y { get { return coord.y;} }
 
     public Slot this[Side index] { // access to neighby slots on the index
         get {
             return nearSlot[index];
         }
     }
 
     public Dictionary<Side, Slot> nearSlot = new Dictionary<Side, Slot> (); // Nearby slots dictionary
     public Dictionary<Side, bool> wallMask = new Dictionary<Side, bool> (); // Dictionary walls - blocks the movement of chips in certain directions
     //public Dictionary<Side, Slot> jellyMask = new Dictionary<Side, Slot> (); // Dictionary jellys
     
     public SlotGravity slotGravity;
     public SlotTeleport slotTeleport;
 
     public bool sugarDropSlot = false;
     public static Transform folder;
     
     void  Awake (){
         slotGravity = GetComponent<SlotGravity>();
         slotTeleport = GetComponent<SlotTeleport>();
 
         Instance = this;
     }
 
     public static void  Initialize (){
         foreach (Slot slot in FindObjectsOfType<Slot>())
             if (!all.ContainsKey(slot.coord))
                 all.Add(slot.coord, slot);
 
 
         foreach (Slot slot in all.Values) {
             foreach (Side side in Utils.allSides) // Filling of the nearby slots dictionary 
                 slot.nearSlot.Add(side, all.ContainsKey(slot.coord + side) ? all[slot.coord + side] : null);
             slot.nearSlot.Add(Side.Null, null);
             foreach (Side side in Utils.straightSides) // Filling of the walls dictionary
                 slot.wallMask.Add(side, false);
         }
 
         Side direction;
         SlotTeleport teleport;
         foreach (Slot slot in all.Values) {
             direction = slot.slotGravity.gravityDirection;
             if (slot[direction]) {
                 slot[direction].slotGravity.fallingDirection = Utils.MirrorSide(direction);
             }
             teleport = slot.GetComponent<SlotTeleport>();
             if (teleport)
                 teleport.Initialize();
         }
     }
 
     Chip _chip;
     public Chip chip {
         get {
             return _chip;
         }
         set {
             if (value == null) {
                 if (_chip)
                     _chip.slot = null;
                 _chip = null;
                 return;
             }
             if (_chip)
                 _chip.slot = null;
             _chip = value;
             _chip.transform.SetParent(transform);
             if (_chip.slot)
                 _chip.slot.chip = null;
             _chip.slot = this;
         }
     }
 
     public void SetScore(float s) {
         int add = Mathf.RoundToInt(s * SessionAssistant.scoreC);
         SessionAssistant.main.score += add;
         ScoreBubble.Bubbling(add, transform);
     }
 
     // Check for the presence of the "shadow" in the slot. No shadow - is a direct path from the slot up to the slot with a component SlotGenerator. Towards must have slots (without blocks and wall)
     // This concept is very important for the proper physics chips
     public bool GetShadow (){
         if (slotGravity) return slotGravity.shadow;
         else return false;
     }
 
     // Shadow can also discard the other chips - it's a different kind of shadow.
     public bool GetChipShadow (){
         Side direction = slotGravity.fallingDirection;
         Slot s = nearSlot[direction];
         for (int i = 0; i < 40; i ++) {
             if (!s) return false;
             if (s.block)  return false;
             if (!s.chip || s.slotGravity.gravityDirection != direction) {
                 direction = s.slotGravity.fallingDirection;
                 s = s.nearSlot[direction];
             } else return true;
         }
         return false;
     }
     
     // creating a wall between it and neighboring slot
     public void  SetWall (Side side){
 
         wallMask[side] = true;
         if (side == Side.Left)
             insideWallL = true;
         if (side == Side.Right)
             insideWallR = true;
         if (side == Side.Bottom)
             insideWallB = true;
         if (side == Side.Top)
             insideWallT = true;
 
     }
 
     public void jellyBreak(Jelly jelly) {
 
         foreach (Side side in Utils.allSides) {
 
             if (this [side].jelly) { 
                 if (side == Side.Left)
                     jelly.jellyL = true;
                 Debug.Log ("found jelly");
                 if (side == Side.Right)
                     jelly.jellyR = true;
                 Debug.Log ("found jelly");
                 if (side == Side.Bottom)
                     jelly.jellyB = true;
                 Debug.Log ("found jelly");
                 if (side == Side.Top)
                     jelly.jellyT = true;
                 Debug.Log ("found jelly");
             }
         }
 
     }
 
     public static Slot GetSlot(int2 position) {
         if (all.ContainsKey(position))
             return all[position];
         return null;
     }
 
     public static Slot GetSlot(int x, int y, int z) {
         return GetSlot(new int2(x, y));
     }
 }



Here is the script where I am calling the function:


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 // Jelly element on playing field
 public class Jelly : MonoBehaviour {
 
     public int level = 1; // Level of jelly. From 1 to 3. Each "JellyCrush"-call fall level by one. If it becomes zero, this jelly will be destroyed.
     public Sprite[] sprites; // Images of jellies of different levels. The size of the array must be equal to 3
     SpriteRenderer sr;
     Animation anim;
     public string crush_effect;
 
     // check condition of jelly location
     public bool jellyR;
     public bool jellyL;
     public bool jellyT;
     public bool jellyB;
 
     
     // Spawn Jelly function
     void Start() {
 
 
         sr = GetComponent<SpriteRenderer>();
         sr.sprite = sprites[level - 1];
         anim = GetComponent<Animation>();
         AnimationSpeed speed = GetComponentInChildren<AnimationSpeed>();
         speed.speed = Random.Range(0.4f, 0.8f);
         speed.offset = Random.Range(0f, 1f);
 
         Slot.jellyBreak (this);
     }
 
 
     // Crush block funtion
     public void JellyCrush (){
         if (level == 1) {
             AudioAssistant.Shot("JellyCrush");
             StartCoroutine(DestroyingRoutine());
             return;
         }
         level--;
         anim.Play("JellyCrush");
         AudioAssistant.Shot("JellyrHit");
         sr.sprite = sprites[level-1];
     }
 
 
 
 
     // Ienumerator Routine to remove GameObjects and play animation "BlockDestroy"
     IEnumerator DestroyingRoutine() {
         GameObject o = ContentAssistant.main.GetItem(crush_effect);
         o.transform.position = transform.position;
 
         anim.Play("BlockDestroy");
         while (anim.isPlaying)
             yield return 0;
 
         Destroy(gameObject);
     }
 }



When calling the function "Slot.jellyBreak (this)" is not working and is throwing the error message. Is there another way to call the function that would not display the error message?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JVene · Aug 02, 2018 at 02:07 AM

You have an odd situation, and while I could question the arrangement, here's what's happening to you.

Here, a member of Jelly calls this:

  Slot.jellyBreak (this);

Slot is a type (the name of a class). If jellyBreak were a static function this call would work, but jellyBreak is a public method (non-static), so an Instance of Slot is required to make the call.

However, you have an instance, called Instance, as a static member of Slot, accessible as Slot.Instance, so:

 Slot.Instance.jellyBreak(this);


Would solve the immediate problem.

However, a word of caution. You have a public function named "this" which is used in jellyBreak, but "this" is a keyword and should not be used. Rename that (I would be surprised if it compiles).

Comment
Add comment · 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
0

Answer by ModLunar · Aug 02, 2018 at 02:12 AM

I think you meant to do Slot.Instance.jellyBreak(this), since the jellyBreak method is an instanced method.

Let me know if you'd like me to share more details about why/what that means :) I hope this helps!

Comment
Add comment · 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

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

533 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Creating a custom mesh - Cleaning the mesh failed 2 Answers

How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ?? 2 Answers

Upgrade C# language version to 7.0 Mac in Unity?,Upgrade project to C# language version '7.0' on a Mac? 1 Answer


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