Hi there,
I have a static function I am using. However, making it static has made life difficult in that, many errors have appeared. Mainly, all errors suggest that "error CS0120: An object reference is required to access non static member ‘myScript.ststicFunction’
I understand that any variable in a static function, itself needs to be static, but that in itself is quite a task and inconvient since there are a good number of objects I would need to switch to static. Let alone, create a non static singleton to go along with the static member.
Anyone know what I am going thru, and can explain or give me an alternative?
Yeah, it sounds to me like your static function is attempting to access a variable or another function that is not static. One thing you could do as a quick and dirty fix is create a class with a static member that is an instance of the class you’re working with. So essentially, make your class a Singleton instead of all static. If your existing class is not static already (which it isn’t) and is fairly complex (which it sounds like it is), then a Singleton pattern would be much easier to implement as you wouldn’t need to rework anything else.
Thanks. My reason for switching was that I was thinking of instantiation a prefab. This prefab, is made up by a couple of dozen coins. When a coin is collected it must access another scripts ufc tiron. So, instead of having each coin, on Awake, or Start, find this object and get its component, my hope was to use a static function.
Yeah I think your best bet is to create a helper “manager” class with a static property that holds a singleton instance of this class. From each of your coins you can then just use the static method on the helper to get the instance of this class and you don’t have to worry about finding the object or using getcomponent.
Ok, I’m not entirely clear on what it is you’re trying to do… but let me give you an example. Let’s say I have this class:
public class CoinClass
{
private string _someString = "foo";
public void SetString(string newValue)
{
_someString = newValue;
}
}
Now, let’s say you want to change SetString to be a “static” function… but you can’t because _someString is not static. I think this is similar to what you’re trying to do… Now you could make _someString (or anything else you needed static)… or you could do something like this:
public static class CoinClassManager
{
private static CoinClass _coinClass;
private static object _lockObject = new object();
public static CoinClass CoinClass
{
get
{
if(_coinClass == null)
{
lock(_lockObject)
{
if(_coinClass == null)
{
_coinClass = new CoinClass();
}
}
}
return _coinClass;
}
}
}
What this will do is create a static Manager that will give every coin access to one CoinClass instance (without CoinClass itself needing to be static). If this is the first time it’s being accessed, it creates a new instance of it. You’ll note that I did two null checks for the _coinClass variable… this is because access locking to prevent concurrency issues between threads (using the lock keyword) is expensive… so if there’s already an instance, no need to lock… if there isn’t one, I lock the next block… then I check again. This way if another thread enters the locked block after the first thread creates the object, it won’t create it again. Then I return it.
So now, each one of your coins can just call:
CoinClassManager.CoinClass.SetString("bar");
So really, you’re just using a “Manager” or whatever you want to call it to give you the shared instance rather than looking it up every time. Now this example works with classes… but you may want to do it with a Component or GameObject… in that case you’ll do the lookup once instead of creating a new instance and store it in your variable. Beware though that you’ll be creating a reference to that component that will prevent it from being garbage collected properly, so on the object where that component exists, you’ll need to add some logic to make sure it gets cleaned up from the Manager as well so it can be collected when need be.
What’s wrong with caching the component reference in Awake? Or, in all honesty, not caching it at all if you only need to call GetComponent once when the player collects it.
That would only be a problem if you were doing it in Update.
tbh, there are other things that im sure youre probably doing that do need correction… namely using Translate on a rigidbody… which I would bet money on you doing.
Using velocity is better than using Translate, but using forces is better than using velocity.
Its kind of annoying that Unity dont provide a nice rigidbody version similar to Translate… I understand why, but forces are a pain in the ass to work with IMO
Hm. So, if I need to move a transform, from a to b, however this transform does have a rigidbody, what do you suggest is the best method? And why if you don’t mind.
Make the rigidbody kinematic. I believe this negates all negative effects of moving a rigidbody using translate, because it excludes it from the physics system. This means collisions wont push other RB’s around though, so if you want realistic physics, this is a poor choice.
Moving with forces is the correct way to do it if you want realistic physics with minimal physic system impact.
The reason using translate is bad, is because it causes complex physics calculations to be re-run. In a system with a low number of rb’s this aint the worst problem in the world, but its not ideal either.