unused assigned variables in Unity character skeleton mapping - unity3d

I have a motion-capture system that I use to animate a character (non-mecanim) so obviously I have to map the motion data onto the character's bones and segments.
Here is how I do it, but since the joints names are only used within the mapBones() method, I get quite a few amber warnings at the Unity console that warning CS0219: The variable JtHips is assigned but its value is never used.
I get one for each bone! It does not look professional, so I am wondering if there is any way I could get rid of them? All the posts I have checked out, of course recommend using them, but in this case, they are being used internally by the mapping function...
private Dictionary<MoCapSegment, ChrBones> jointsDict;
public enum MoCapSegment
{
Pelvis = 0,
// many more ...
Neck = 48,
Head = 49
}
public enum ChrBones
{
JtHips = 0,
// many more ...
JtNeckA = 48,
JtSkullA = 49
}
protected void mapBones()
{
jointsDict = new Dictionary<MoCapSegment, ChrBones>();
GameObject JtHips = transform.Find("CharacterRoot/JtJtHips").gameObject;
jointsDict.Add(MoCapSegment.Pelvis, ChrBones.JtHips);
// all the rest
}

You CAN do the following, but be careful
#pragma warning disable 0414
Specifically,
#pragma warning disable 0168// variable declared but not used.
#pragma warning disable 0219// variable assigned but not used.
#pragma warning disable 0414// private field assigned but not used.
I agree with you that this problem is infuriating.
Another thing you can do is this...
public void check<T>(T x){}
and then you can
private Whatever JtHips = whatever; // throws warning since never used
check(JtHips); // avoid warning like this
As always it's a great chance to use an extension
public static class YourExtensions
{
public static void Nothing<T>(this T x) {}
and then you can
JtHips.Nothing();
JtNeck.Nothing();
JtSkull.Nothing();
etc...
All that being said, it's a very bad idea to suppress warnings, of course. Take care.

Related

Storing script names in a variable

new to unity so please ignore if I sound stupid.
I have to scripts references in a script. Example, script A and B are referenced in script C.
I need to store script A and B variable names in a another variable so that I can use that variable in conditioning.
private FemalePlayerAnimations femalePlayerAnimations;
private MalePlayerAnimations malePlayerAnimations;
private Variable variable; // Got Problem Here
void Awake()
{
femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}
void Start()
{
if(1 + 1 = 2) // Some Condition
{
variable = femalePlayerAnimations;
}
else if(1 + 2 = 3) // Some Another Condition
{
variable = malePlayerAnimation;
}
}
Thanks in advance.
If I understand your question correctly, you'll need to use inheritence and have your male/female animations inherit from the same base class.
i.e.
public abstract class BasePlayerAnimator : MonoBehavior {}
public class MalePlayerAnimator : BasePlayerAnimator {}
public class FemalePlayerAnimator : BasePlayerAnimator {}
Real question though is why do you need two different classes for male/female animations? wouldn't a single class with 2 different instances cover your needs?
I have a feeling you don't simply want the name of the variable as a string.
The logic you are trying to implement here won't work. You can't have a variable that holds the FemalePlayerAnimations class hold a MalePlayerAnimations class.
You should reconsider the design of your program as you can have two different instances (prefabs) of the same theoratical PlayerAnimations class. This is how Animation Controllers work in Unity.
Alternatively you could use a boolean field to store states, for example: bool useFemaleAnimations that is changed in the conditions and implement the correct "script" where applicable.

How can I properly create invalid Vector3 in Unity?

I often need to pass invalid location to functions that optionally can use location (eg. RTS unit action). But this does not compile:
public abstract void CastSpell(Spell spell, Vector3 targetLocation = null);
So how can I make a proper invaid Vector3 to determine invalid/"don't care" locations? I've seen things like vector with -999999 coordinates - but that's a nasty trick that might cause bugs (eg. on huge maps).
I had a similar problem in the past, I found there was two solutions. The first solution was to create a wrapper for the class.
Class Vector3Wrapper
{
Vector3 vector;
}
Then I would simply set the Vector3Wrapper to null then if it wasnt null access its vector. A better method would be making it a Nullable type. A example is below.
http://unitypatterns.com/nullable-types/
public class Character : MonoBehaviour
{
//Notice the added "?"
Vector3? targetPosition;
void MoveTowardsTargetPosition()
{
//First, check if the variable has been assigned a value
if (targetPosition.HasValue)
{
//move towards targetPosition.Value
}
else
{
//targetPosition.Value is invalid! Don't use it!
}
}

Trying to access variables from another script in unity (Not Homework)

I am currently playing around in Unity trying to make/test a 2D game. I keep getting the following error when I attempt to access CharacterMotor.playerx from inside camerafollow.js:
An instance of type "CharacterMotor" is required to access non static member "playerx"
Here are my two scripts:
camerafollow.js
#pragma strict
function Start () {
transform.position.x = CharacterMotor.playerx;
}
CharacterMotor.js
#pragma strict
#pragma implicit
#pragma downcast
public var playerx : float = transform.position.x;
You could change playerx to static, but I don't think that's what you want to do (there's probably only one player object, but this would prevent you from ever having multiple CharacterMotors). I think you want/need to retrieve the instance of CharacterMotor that is attached to this gameObject.
#pragma strict
function Start () {
var charMotor : CharacterMotor = gameObject.GetComponent(CharacterMotor);
transform.position.x = charMotor.playerx;
}
An instance of type "CharacterMotor" is required to access non static member "playerx"
The above error message describes precisely what is happening. You are just trying to access a variable without first creating an instance of it. Keep in mind that UnityScript != JavaScript.
To fix this issue, simply change
public var playerx : float = transform.position.x;
to
public static var playerx : float = transform.position.x;
Though this fixes your immediate problem I do not recommend continuing down this path. I suggest that you learn other aspects of the language first (such as classes) so that you can better organize and construct your data.
See: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
CharacterMotor is the type, there can be multiple instantiations of your type in memory at the same time so when you call the type name you are not referencing any instance in memory.
to get an instance of the type that is connected to you current gameobject try this:
var charactorMotor : CharacterMotor = gameObject.getComponent("CharacterMotor");
Now you have access to that instances properties
transform.position.x = characterMotor.playerx;

Finally Action on PostSharp Exception aspect

I got following situation
private volatile bool _inProgress = false;
public void DoSomethingStart()
{
if(_inProgress == false)
{
foo.BeginInvoke(null, null); // DoSomething
_inProgress = true;
}
}
[CatchAllExceptionsFromHere]
private void DoSomething()
{ }
The aspects works so far. All exceptions are handeled from CatchAllExceptionsFromHere. But i want to set "_inProgress = false" in the finally clause of the aspect - so if DoSomething has finished "_inProgress" should be set to false. Since Attributes cant take any object - is there a workaround?
Thanks michael
Aspects can take values on the declaration to set property values or just like a constructor. What you want is an OnMethodBoundaryAspect and in the OnExit method (which fires even if an error has occured, so it's just like a finally) you want to set the value of _inProgress.
However, the aspect needs to be an instance aspect and you'll need to either import the _inProgress member or just introduce it so that the aspect has access to it. It depends on if your class needs access to it. If it does the have the aspect import the member. Read up on how to do these things with these links
Aspect lifetime & scope part 1
Aspect lifetime & scope part 2
Introducing members part 1
Introducing members part 2

Setter Getter oddness #property

I am having a really odd problem trying to set a simple float value to 1.
My property:
{
float direction;
}
#property(nonatomic)float direction;
Which is synthesized:
#synthesize direction;
I then used the following code:
- (void)setDirection:(float)_direction {
NSLog(#"Setter called with value of %f",_direction);
self->direction = _direction;
}
For testing purposes...
When I try to change the value with this,
[[w getCharacter] setDirection:1.0f];
where [w getCharacter] gives this:
return [[[self scene] gameLayer] player];
I get the warning, "setDirection not defined."
If I switch to dot notation([w getCharacter].direction), I get "confused by earlier errors, bailing out".
Here is where the weirdness starts. When I try to change the value, the debug message displays _direction = 0.00000. When I check the number later, its still 0.000. I am clearly trying to change it to 1, why is this not working?
The simplest explanation is that [w getCharacter] doesn't return the class of object you think it does. Only the class that has direction defined for it can respond to the message. You should test this by explicitly calling it with the class it defined for.
It is possible you did not include the header that defines the method.
Two probably unrelated issues:
The self->direction construction will work for a scalar value but it does an end run around the entire class concept. In this case just use: 'direction=_direction;` and it will set it directly.
Apple reserves all names that start with underscores for its own internal use. You should not use them because Objective-c has a global name space. It's possible that you can accidentally use an Apple variable that is defined deep within a framework. (This is why framework constants all start with NS,CF,CA etc.)
[Note: In the Comments, the author says to ignore this answer.
self.direction = 1; is syntactic sugar for
[self setDirection: 1];
when you call
-(void)setDirection:(float)_newDirection {
self.direction = _newDirection;
}
You seem to be telling the compiler or preprocessor to set up a recursive loop for you. The preprocessor (I think) changes it to this:
-(void)setDirection:(float)_newDirection {
[self setDirection: _newDirection];
}
If you call it simply
-(void)setDirection:(float)_newDirection {
direction = _newDirection;
}
the assignment should work (it worked for me just now)