- 投稿日:2021-09-01T20:14:53+09:00
【Unityメモ】The script don't inherit a native class that can manage a script.
Unity上で新しい C#スクリプトを作成し、オブジェクトにアタッチしようとしたら以下のエラーが出た。 The script don't inherit a native class that can manage a script. 調べると、スクリプト名とクラス名が一致していないことが原因とわかったが、何度確認しても二つが一致している。 Hoge.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Hoge : MonoBehaviour { private void Start() { Debug.Log("Start"); } } スクリプトの作り直しやアタッチし直しをしても治らず。 結果、全く別のスクリプトのエラー(セミコロンつけ忘れ)が原因だった。 エラーを読んでも修正箇所がわからなかったのでメモ。
- 投稿日:2021-09-01T19:18:26+09:00
CSharpZxScriptでbatファイルからの脱出
はじめに 心地よく書けるC#スクリプトとしてCSharpZxScriptを作ってみました。 dotnet tool経由で簡単に導入出来ます。 心地よく書ける C#でスクリプトが書ける物で公式のcsxがありますがVisualStudioでコード補完が効きにくかったり書きにくいです。 そこで内部でこっそり.csproj を生成したりします。 C#9.0のTop Level StatementとProcessXを組み合わせると中々良い感じに書けます。 デバッガーも繋げます。 動作環境 必要な環境は.NET5.0 (C#9.0 が必要なため) WindowsでもMacでもLinuxでも動きます。 CLIによる各種操作の実行 GUI操作での実行(Windowsのみ 導入方法 dotnet toolコマンドで導入を行います。 dotnet tool install --global CSharpZxScript cszx コマンドが使えるようになります。 各種cszxコマンドは helpを見てください。 installをしてダブルクリックや右クリックメニューから動かせるようにします。 cszx install 消すときは cszx uninstall で消えます 使い方 .csファイルを右クリックしてEdit or Run します。 .cszxに拡張子を変えるとダブルクリックで実行出来るようになります。 ただし.cszxにしてしまうとコード補完が効かなくなり編集しにくいです コマンドからの起動は拡張子を明示しないほうがおすすめです。 編集時は.cs、Fixするときに.cszxとやりやすいので。 cszxスクリプトからcszxスクリプトを呼び出す時の面倒回避にもなります。 cszx test おわり 私のプロジェクトはCSharpZxScriptをインストールするbatファイル以外全て置き換えました。 本来の用途ではないんですが単純なアプリケーションをこいつで書くのが結構便利でした。 「FileServerとClientを両方Core.csproj参照してExe作っている」という状況があったのでスクリプト化するとスッキリしました。
- 投稿日:2021-09-01T12:21:15+09:00
Unity:オブジェクトの動きの設定とprefabのclone作成
Unity備忘録 Objectの移動 void Move() { transform.position += new Vector3(0, 4.5f, 0) * Time.deltaTime; } 上記スクリプトでy軸方向に一定速度でオブジェクトが移動する。 void Move() { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); transform.position += new Vector3(x, y, 0) * Time.deltaTime * 4f; } 方向指示キーでオブジェクトを動かす場合、Input.GetAxisをnew Vector3の中で受け取る。 Prefabのクローニング void Clone() { if (Input.GetKeyDown(KeyCode.Space)) { Instantiate(Prefab, PrefabPoint.position, PrefabPoint.rotation); } } 取得したPrefub Objectをスペースキーが押されるときにInstantiateで呼び出す場合、上記のように記載。呼び出す場所の位置(position)と回転(rotation)も指定する必要がある。 実装 プレイヤーを方向指示キーで動かし、玉の発射の実装。慣れると、10分ぐらいで実装可能。Unity C#は慣れるとわかりやすい言語だな再認識。#スタジオしまづ https://t.co/8QWBZ399xV pic.twitter.com/iwM6WmCnMr— Unity勉強中の整形外科医:北城雅照|医療者向けプログラミングスクール「もいせん」開校! (@teru3_kitashiro) September 1, 2021 実際の動きと、書いたスクリプト PlayerShip.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerShip : MonoBehaviour { public GameObject BulletPrefab; public Transform FirePoint; // Update is called once per frame void Update() { Move(); Shot(); } void Move() { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); transform.position += new Vector3(x, y, 0) * Time.deltaTime * 4f; } void Shot() { if (Input.GetKeyDown(KeyCode.Space)) { Instantiate(BulletPrefab, FirePoint.position, FirePoint.rotation); } } } Bullet.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { // Update is called once per frame void Update() { ShotMove(); } void ShotMove() { transform.position += new Vector3(0, 4.5f, 0) * Time.deltaTime; } }