プロが教えるわが家の防犯対策術!

最近unity2dで横スクロールアクションを勉強していて
今移動するなどのアニメーションを動かそうとしています
しかし待機状態から移動するときのアニメーションに移行するスクリプトがうまくいきません
誰か教えてください
error CS0411: The type arguments for method `UnityEngine.Component.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitlyというエラーが出ます
プログラムはこんな感じです

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class UnityChanController : MonoBehaviour {

public Rigidbody2D cRigidbody2D
{
get
{
if(!_cRigidbody2D)
_cRigidbody2D = rigidbody2D;
return _cRigidbody2D;
}
}
Rigidbody2D _cRigidbody2D;

public Transform cTransform
{
get
{
if(!_cTransform)
_cTransform = transform;
return _cTransform;
}
}
Transform _cTransform;
public Animator cAnimator
{
get
{
if(!_cAnimator)
_cAnimator = GetComponent();
return _cAnimator;
}
}
Animator _cAnimator;
public float moveSpeed = 5;
public float jumpForce = 1000;

float InputHorValue;

bool isGrounded;
bool canJump;

void Update()
{
MecCheck();
InputCheck();
}

void InputCheck()
{
InputHorValue = Input.GetAxisRaw("Horizontal");
if(isGrounded && Input.GetButtonDown("Jump")) canJump = true;
}

void MecCheck()
{
bool isRunning = InputHorValue != 0;
cAnimator.SetBool("isRunning",isRunning);
}

void FixedUpdate()
{
Move();
Jump();
}

void Move()
{
if((cTransform.localScale.x > 0 && InputHorValue < 0)
|| (cTransform.localScale.x < 0 && InputHorValue > 0))
{
Vector2 temp = cTransform.localScale;
temp.x *= -1;
cTransform.localScale = temp;
}
cRigidbody2D.velocity = new Vector2(moveSpeed * InputHorValue,
cRigidbody2D.velocity.y);
}

void Jump()
{
if(canJump)
{
canJump = false;
isGrounded = false;
cRigidbody2D.AddForce(Vector2.up * jumpForce);
}
}

void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "Ground")
isGrounded = true;
}
}

A 回答 (3件)

~~~~~~~~~~


public Animator cAnimator
{
get
{
if(!_cAnimator)
_cAnimator = GetComponent();
return _cAnimator;
}
}
Animator _cAnimator;
~~~~~~~~~~

~~~~~~~~~~
public Animator cAnimator
{
get
{
if(!_cAnimator)
_cAnimator = GetComponent<Animator>();
return _cAnimator;
}
}
Animator _cAnimator;
~~~~~~~~~~

こうするといいかも!?
    • good
    • 1
この回答へのお礼

やってみたら動きました!!
感動;;
ありがとうございます!

お礼日時:2014/08/19 15:40

ざっと見ただけですが、GetComponentの総称型にクラスを指定する、ということでは。



_cAnimator = GetComponent();

_cAnimator = GetComponent<Animator>();


他にもプロパティでチェックしているフラグ変数はどうなっているのかとか、プロパティはいつ初期化されてるのかとか、不明なところがあるので動かしてはいませんが……。
    • good
    • 0
この回答へのお礼

できました!!!!!
ありがとうございます

お礼日時:2014/08/19 15:39

Unityはあまり触っていないので、違っていたら申し訳ないですが、


cAnimatorプロパティのgetアクセサの
 _cAnimator = GetComponent();

 _cAnimator = GetComponent<Animator>();
にしてみてはどうでしょう?


※C++ではなくC#です
    • good
    • 0
この回答へのお礼

C#でしたかw
ありがとうございます
結構初歩的なミスだってのでしょうか…

お礼日時:2014/08/19 15:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!