unityでのC++エラーの原因がわかりません!!

このQ&Aのポイント
  • unityでのC++エラーの原因がわかりません。最近unity2dで横スクロールアクションを勉強していて、移動するアニメーションがうまくいきません。誰か教えてください。
  • エラーCS0411が発生しています。`UnityEngine.Component.GetComponent<T>()`メソッドの型引数が推論できません。型引数を明示的に指定してみてください。
  • 上記のエラーメッセージが表示されるプログラムの一部を共有します。
回答を見る
  • ベストアンサー

unityでのC++エラーの原因がわかりません!!

最近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; } }

質問者が選んだベストアンサー

  • ベストアンサー
  • anmochi
  • ベストアンサー率65% (1332/2045)
回答No.2

~~~~~~~~~~ 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; ~~~~~~~~~~ こうするといいかも!?

kanzito-ku
質問者

お礼

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

その他の回答 (2)

noname#247307
noname#247307
回答No.3

ざっと見ただけですが、GetComponentの総称型にクラスを指定する、ということでは。 _cAnimator = GetComponent(); ↓ _cAnimator = GetComponent<Animator>(); 他にもプロパティでチェックしているフラグ変数はどうなっているのかとか、プロパティはいつ初期化されてるのかとか、不明なところがあるので動かしてはいませんが……。

kanzito-ku
質問者

お礼

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

  • Picosoft
  • ベストアンサー率70% (274/391)
回答No.1

Unityはあまり触っていないので、違っていたら申し訳ないですが、 cAnimatorプロパティのgetアクセサの  _cAnimator = GetComponent(); を  _cAnimator = GetComponent<Animator>(); にしてみてはどうでしょう? ※C++ではなくC#です

kanzito-ku
質問者

お礼

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

関連するQ&A

  • Unityの質問です。

    ボタンを押すと、キャラクターの真後ろにオブジェクトが出現するようにしたいのですが、 using UnityEngine; using System.Collections; [ExecuteInEditMode()] public class GUIController : MonoBehaviour { public CharacterController characterController; public GameObject character; public GameObject domino; private Vector3 objPosition; // Use this for initialization void Start () { objPosition = new Vector3 (0, 0, 0); } void OnGUI(){ if(GUI.Button(new Rect(600, Screen.height - 160, 120, 120), "おす")){ Debug.Log("ぼたん"); float x = characterController.transform.localPosition.x; float y = characterController.transform.localPosition.y + 0.5f; float z = characterController.transform.localPosition.z - 0.64f; objPosition.Set(x, y, z); Debug.Log (x+","+y+","+z); GameObject domino_prefab = (GameObject)Instantiate(domino,objPosition, characterController.transform.rotation); } } } こうしてみたのですが、うまくキャラクターの真後ろにオブジェクトが出てきてくれません。 どなたか助けてください!

  • コンパイルすると error C1083 が出てしまいます

     -スペック- Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM Microsoft .NET Framework Version 3.5 SP1 ---------------------------------------------------------------- 以下のプログラムを実行すると、error C1083: include ファイルを開けません。'myhelper.h': No such file or directory っていうエラーが出ます。 ----------------------------------------------------------------  -プログラム-  - mymain.cpp の中身 - #include "myhelper.h" #include "mymain.h" //自キャラのデータ Point2D g_jikipos = {40, 400}; //自キャラの座標 //画像ハンドル int g_jikiimage[11]; //色々なファイルの読み込み int LoadFiles(){ //画像ファイル読み込み if(LoadDivGraph("media\\player01.bmp", 11,11,1,64,64,g_jikiimage) == -1) return -1; return 1; }  - mymain.h の中身 - //他から呼び出させるMyMainの関数 void MyMain(); int LoadFiles();  - myhelper.h の中身 - #include "DxLib.h" #include <limits.h> #include <math.h> //構造体宣言 //座標またはベクトルを記録する構造体 struct Vector{ float x,y; }; typedef Vector Point2D; //線を記録する構造体 struct Line2D{ Point2D startpos, endpos; float katamuki; //傾きをラジアン値で記録 Vector speed; //移動している場合は速度をセット }; //球体を記録する構造体 struct Ball2D{ Point2D position; float hankei; //半径 }; //四角形を記録する構造体 struct Rect2D{ Point2D lefttop; Point2D rightbottom; float width; float height; }; //ライブラリ関数 Point2D PosInView(Point2D in); int XInView(float inx); int YInView(float iny); void ScrollToLeft(float jikiposx); void ScrollToRight(float jikiposx); void ScrollToUp(float jikiposy); void ScrollToDown(float jikiposy); void DrawLineInView(float x1, float y1, float x2, float y2, int Color, int Thickness); void DrawCircleInView(float x, float y, float r, int Color, int FillFlag); void DrawAnimation(float x, float y, double ExtRate, double Angle,int TurnFlag, int *imgarray, int allframe, float fps); //ベクトル関数 Vector CreateVector(Vector in, float veclen); Vector AddVector(Vector v1, Vector v2); Vector SubVector(Vector v1, Vector v2); Vector AddVectorInFrameTime(Vector pos, Vector speed); Vector AddVectorInFrameTime2(Vector pos, Vector speed, Vector accel); Vector Normalize(Vector in); Vector RotateVector(Vector in, float radian); float VectorLengthSquare(Vector in); float DotProduct(Vector v1, Vector v2); float CrossProduct(Vector v1, Vector v2); void SetLine2DKatamuki(Line2D *in); void DrawLine2D(Line2D in, int Color, int Thickness); void DrawBall2D(Ball2D in, int Color, int Fill); //当たり判定関数 bool HitTestLineAndBall(Line2D linein, Ball2D ballin); bool IsPointAtLineFace(Line2D linein, Point2D ptin); bool HitTestLineAndLine(Line2D line1, Line2D line2); bool HitTestBallAndBall(Ball2D a, Ball2D b); bool HitTestPointAndBox(Rect2D rect, Point2D pt); //タイマー関数 void SetSimpleTimer(int idx, int time); int GetPassedTime(int idx); //グローバル変数 extern float g_frametime; extern Rect2D g_framerect; //画面領域(当たり判定) extern Point2D g_current_field_pos; //現在の左上座標 extern Rect2D g_stagesize; //ステージサイズ //定数宣言 const float ZEROVALUE = 1e-10f; const float PIE = 3.1415926f; const int SCROLL_LIMIT = 200; myhelper.h はコピペなので間違ってはない。 ---------------------------------------------------------------- まだC言語初心者なので、どのようにすれば直せるのかわからないので 詳しくお願いします。 一応サイトで検索などをして、自分なりに調べてきて 定義が間違ってないかを見ましたけどそれでもまだわかりません。 それでは宜しくお願いします。

  • unityでのプログラミングについて

    いまunityでゲームを制作していてシューティングのゲームをつくっています。 そこで次のようなプログラムを作成したのですがエラーが返ってきて困っています。 var bulletPrefab : GameObject; var bulletInterval : float = 0.2; var bulletEnable : boolean = true; var bulletVelocity : float = 100; var bulletTime : float = 0.0; (省略してます) function start(){ (省略してます) function update(){ if(Input.GetButton("Jump") && bulletEnable) { ←エラー箇所です。 bulletEnable = false; var bullet = Instantiate(bulletPrefab,transform.position,transform.rotation); var direction : Vector3 = Vector3(0,0,1); bullet.rigidbody.AddForce(direction * bulletVelocity,ForceMode.VelocityChange); } bulletTime += Time.deltaTime; if (bulletTime &gt;bulletInterval) { ←エラー箇所です。 bulletTime = 0.0; bulletEnable = true; } } 次のエラーが返ってきます。 Assets/character.js(22,19): BCE0044: expecting ), found ';'. Assets/character.js(22,20): BCE0043: Unexpected token: bulletInterval. こんな感じでエラーがでます。 解決方法を知ってる方は教えてください。おねがいします。

  • Unity について

    Unityでゲームを作っているのですがどうしてもできない部分があり困っています それは 転がる玉にあわせてカメラが動かないのです 転がる玉を作るとカメラが追跡しなく 玉を追ってカメラが追跡するようにすると球が転がりません 理想は転がる玉を一定の距離を保ったまま追跡することなのですが ぜんぜんうまくいかないのです --玉は転がるがカメラが追跡しない方 using UnityEngine; using System.Collections; public class PlayerBehaviour : MonoBehaviour { private void OnCollisionEnter(Collision collision) { Debug.Log("kabe hito"); } public float jumpForce = 10; public float Up = 10; public float Down = 10; public float Right = 10;     public float Left = 10; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.Space)) {      rigidbody.AddForce(0,jumpForce,0); Debug.Log("jump! force=" + jumpForce ); } if(Input.GetKey(KeyCode.UpArrow)) { rigidbody.AddForce(0,0,-Up); Debug.Log("UP force=" + Up ); } if(Input.GetKey(KeyCode.DownArrow)) {      rigidbody.AddForce(0,0,Down); Debug.Log("Down force=" + Down ); } if(Input.GetKey(KeyCode.RightArrow)) { rigidbody.AddForce(-Right,0,0); Debug.Log("Right force=" + Right ); } if(Input.GetKey(KeyCode.LeftArrow)) { rigidbody.AddForce(Left,0,0); Debug.Log("Left force=" + Left ); } } } ------カメラが追跡するが玉は移動するが転がらない方 玉にComponent→Character→Platform Input Controllerを選択 そして玉にJavascriptのThirdPersonCamera 1を入れる ThirdPersonCamera 1の中身は http://forum.unity3d.com/threads/70148-Rotation-around-character-by-pressing-RIGHT-MOUSE-BUTTON のThirdPersonControllerの部分をCharacterControllerに変えたやつ 長くなってしまいましたがほんとに困ってます 助けてください

  • LNK2019: 未解決の外部シンボルのエラーが出る

    Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM Microsoft .NET Framework Version 3.5 SP1 ---------------------------------------------------------------- 新しいプリジェクト→Win32 コンソール アプリケーション(ソリューションのディレクトリを作成 チェック外す)→Windows アプリケーション(空のプロジェクト チェック外す) ----------------------------------------------------------------  プログラム  mymain.cpp #include "myhelper.h" #include "mymain.h" //自キャラのデータ Point2D g_jikipos = {40, 400}; //自キャラの座標 //画像ハンドル int g_jikiimage[11]; //色々なファイルの読み込み int LoadFiles(){ //画像ファイル読み込み if(LoadDivGraph("media\\player01.bmp", 11,11,1,64,64,g_jikiimage) == -1) return -1; return 1; }  mymain.h //他から呼び出させるMyMainの関数 void MyMain(); int LoadFiles();  myhelper.h(サンプルなので打ちミスはない) #include "DxLib.h" #include <limits.h> #include <math.h> //構造体宣言 //座標またはベクトルを記録する構造体 struct Vector{ float x,y; }; typedef Vector Point2D; //線を記録する構造体 struct Line2D{ Point2D startpos, endpos; float katamuki; //傾きをラジアン値で記録 Vector speed; //移動している場合は速度をセット }; //球体を記録する構造体 struct Ball2D{ Point2D position; float hankei; //半径 }; //四角形を記録する構造体 struct Rect2D{ Point2D lefttop; Point2D rightbottom; float width; float height; }; //ライブラリ関数 Point2D PosInView(Point2D in); int XInView(float inx); int YInView(float iny); void ScrollToLeft(float jikiposx); void ScrollToRight(float jikiposx); void ScrollToUp(float jikiposy); void ScrollToDown(float jikiposy); void DrawLineInView(float x1, float y1, float x2, float y2, int Color, int Thickness); void DrawCircleInView(float x, float y, float r, int Color, int FillFlag); void DrawAnimation(float x, float y, double ExtRate, double Angle,int TurnFlag, int *imgarray, int allframe, float fps); //ベクトル関数 Vector CreateVector(Vector in, float veclen); Vector AddVector(Vector v1, Vector v2); Vector SubVector(Vector v1, Vector v2); Vector AddVectorInFrameTime(Vector pos, Vector speed); Vector AddVectorInFrameTime2(Vector pos, Vector speed, Vector accel); Vector Normalize(Vector in); Vector RotateVector(Vector in, float radian); float VectorLengthSquare(Vector in); float DotProduct(Vector v1, Vector v2); float CrossProduct(Vector v1, Vector v2); void SetLine2DKatamuki(Line2D *in); void DrawLine2D(Line2D in, int Color, int Thickness); void DrawBall2D(Ball2D in, int Color, int Fill); //当たり判定関数 bool HitTestLineAndBall(Line2D linein, Ball2D ballin); bool IsPointAtLineFace(Line2D linein, Point2D ptin); bool HitTestLineAndLine(Line2D line1, Line2D line2); bool HitTestBallAndBall(Ball2D a, Ball2D b); bool HitTestPointAndBox(Rect2D rect, Point2D pt); //タイマー関数 void SetSimpleTimer(int idx, int time); int GetPassedTime(int idx); //グローバル変数 extern float g_frametime; extern Rect2D g_framerect; //画面領域(当たり判定) extern Point2D g_current_field_pos; //現在の左上座標 extern Rect2D g_stagesize; //ステージサイズ //定数宣言 const float ZEROVALUE = 1e-10f; const float PIE = 3.1415926f; const int SCROLL_LIMIT = 200; ----------------------------------------------------------------  エラー内容 1>myhelper.obj : error LNK2019: 未解決の外部シンボル "void __cdecl MyMain(void)" (?MyMain@@YAXXZ) が関数 _WinMain@16 で参照されました 1>C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\my\Debug\my.exe : fatal error LNK1120: 外部参照 1 が未解決です 1>my - エラー 2、警告 0 ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ---------------------------------------------------------------- 画像を貼り付けときます (見えにくい場合→http://www.dotup.org/uploda/www.dotup.org154142.jpg.html) 初心者なのでわかりやすくお願いします

  • C++ 法線の計算

    Depthmapをもつ画像のピクセルごとに法線を計算する関数を作成したのですが、いまいち計算が遅いです。if文をできるだけ使わないようにしたり、自分なりに変更したのですが、どなたかアドバイスを頂けないでしょうか。 Visual Studio, windows7, C++ です。 Tvector3<float> calc_normal(Tvector3<float> p1, Tvector3<float> p2, Tvector3<float> p3){ Tvector3<float> v1; Tvector3<float> v2; Tvector3<float> cross; float length; Tvector3<float> n; v1 = p1 - p2; v2 = p3 - p2; cross = v2.cross(v1); /* 外積v2×v1の長さ|v2×v1|(= length)を求める */ cross.normalize(); /* 長さ|v2×v1|が0のときは法線ベクトルは求められない */ if (length == 0.0f) { //cout<<"lenth=0"<<endl; //break (); } /* 外積v2×v1を長さ|v2×v1|で割って法線ベクトルnを求める */ //cout<<cross<<endl; return cross; } void calc_all_noraml(Ttexturef dmap, vector<Tvector3<float>> &n){ int w = dmap.getWidth(); int h = dmap.getHeight(); Tvector3<float> pc,pl,pr,pd,pu; int j=0; cout<<j<<endl; for(int i=0; i<w; i++){ vector<Tvector3<float>> temp; pc = Tvector3<float>(i,j,return_depth(dmap,i,j)); if(i-1 > 0)pl = Tvector3<float>(i-1,j,dmap.get(i-1,j).r); if(i+1 < w)pr = Tvector3<float>(i+1,j,dmap.get(i+1,j).r); pu = Tvector3<float>(i,j+1,dmap.get(i,j+1).r); if(i-1 > 0)temp.push_back(calc_normal(pl,pu,pc)); if(i+1 < w)temp.push_back(calc_normal(pc,pu,pr)); Tvector3<float> sum(0,0,0); for(int k=0; k<(int)temp.size(); k++){ sum += temp.at(k); } sum.normalize(); //cout<<sum<<endl; n.push_back(sum); } for(j=1;j<(h-1);j++){ cout<<j<<endl; int i=0; vector<Tvector3<float>> temp; pc = Tvector3<float>(i,j,return_depth(dmap,i,j)); pr = Tvector3<float>(i+1,j,return_depth(dmap,i+1,j)); pu = Tvector3<float>(i,j+1,return_depth(dmap,i,j+1)); pd = Tvector3<float>(i,j-1,return_depth(dmap,i,j-1)); temp.push_back(calc_normal(pc,pu,pr)); temp.push_back(calc_normal(pc,pr,pd)); Tvector3<float> sum(0,0,0); for(int k=0; k<(int)temp.size(); k++){ sum += temp.at(k); } sum.normalize(); n.push_back(sum); temp.clear(); for(int i=1; i<(w-1); i++){ Tvec

  • unity fの意味

    unityでこういうスクリプトを書きました。 ~~~~~~ using UnityEngine; using System.Collections; public class IfStatements : MonoBehaviour { float coffeeTemperature = 85.0f; float hotLimitTemperature = 70.0f; float coldLimitTemperature = 40.0f; void Update () { if(Input.GetKeyDown(KeyCode.Space)) TemperatureTest(); coffeeTemperature -= Time.deltaTime * 5f; } void TemperatureTest () { // If the coffee's temperature is greater than the hottest drinking temperature... if(coffeeTemperature > hotLimitTemperature) { // ... do this. print("Coffee is too hot."); } // If it isn't, but the coffee temperature is less than the coldest drinking temperature... else if(coffeeTemperature < coldLimitTemperature) { // ... do this. print("Coffee is too cold."); } // If it is neither of those then... else { // ... do this. print("Coffee is just right."); } } } ~~~~~~~~~ ですが、Consoleには冷たいコーヒーしか出て来ません。 float coffeeTemperature = 85.0f; float hotLimitTemperature = 70.0f; float coldLimitTemperature = 40.0f; これってどういう意味なのでしょうか? 85.0f;って? 回答お願いします。

  • MeshFilterは何を濾過する?

    unity で「あさって」な質問なのですが、例えば以下のように使われる「MeshFilter」ですが、Filterというと日本語では「濾過(ろか)器」とか「こし器」の意味じゃないですか。 では、MeshFilterは何を濾過しますか?(何を濾過しているイメージですか?) 下の例では、作成したメッシュをsharedMeshプロパティにセットしているだけで、「何かをこしている」感じはしないのですが・・・。 すみません、初心者なもので、概念的なところを教えていただければ、ありがたいです。 よろしくお願いします。 using UnityEngine; [RequireComponent (typeof(MeshRenderer))] [RequireComponent(typeof(MeshFilter))] public class OneTriangle : MonoBehaviour { [SerializeField] private Material mat; void Start() { var mesh = new Mesh(); mesh.vertices = new Vector3[] { new Vector3 (0, 1f), new Vector3 (1f, -1f), new Vector3 (-1f, -1f), }; mesh.triangles = new int[] { 0, 1, 2 }; mesh.colors = new Color[] { Color.white, Color.red, Color.green }; mesh.RecalculateNormals(); var filter = GetComponent<MeshFilter>(); filter.sharedMesh = mesh; var renderer = GetComponent<MeshRenderer>(); renderer.material = mat; } }

  • C# こういう場合ってどうなるの?

    public void A(bool b) {   if(b ? B() : C())//分かると思いますが、三項演算子   {     //略   } } public bool B() {   return true; } public bool C() {   return false } 上記のAにtrueを渡し実行した場合、 if文の中はどうなるのでしょうか? 引数がtrueならBを実行し、Bの戻り値trueが if文で処理され、if内に入る・・・でよろしいのでしょうか? それとも引数bの評価がそのままifで使われるのでしょうか? 例えばのコードなので、bだろうがBだろうが変わらない・・・みたいな 回答はご遠慮ください。

  • unity スクリプト 意味

    // Patrol.cs using UnityEngine; using System.Collections; public class Patrol : MonoBehaviour { public Transform[] points; private int destPoint = 0; private NavMeshAgent agent; void Start () { agent = GetComponent<NavMeshAgent>(); // Disabling auto-braking allows for continuous movement // between points (ie, the agent doesn't slow down as it // approaches a destination point). agent.autoBraking = false; GotoNextPoint(); } void GotoNextPoint() { // Returns if no points have been set up if (points.Length == 0) return; // Set the agent to go to the currently selected destination. agent.destination = points[destPoint].position; // Choose the next point in the array as the destination, // cycling to the start if necessary. destPoint = (destPoint + 1) % points.Length; } void Update () { // Choose the next destination point when the agent gets // close to the current one. if (agent.remainingDistance < 0.5f) GotoNextPoint(); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~ // Choose the next point in the array as the destination, // cycling to the start if necessary. destPoint = (destPoint + 1) % points.Length; の意味がよく分かりません。説明して貰えませんか? このチュートリアル(http://docs.unity3d.com/jp/current/Manual/nav-AgentPatrol.html)です。

専門家に質問してみよう