- Home /
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?
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).
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!