質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

610閲覧

ほかのスクリプトの値を変更したいがエラーが出る

Aya7

総合スコア14

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/10/17 01:52

前提・実現したいこと

AIBehaviorというアセットのhealthの値をほかのスクリプトから変更したい思っています。
この方法以外でダメージを与えられる方法があるのなら教えていただけると助かります。

発生している問題・エラーメッセージ

Assets\script\werpon1.cs(11,43): error CS0246: The type or namespace name 'AIBehaviors' could not be found (are you missing a using directive or an assembly reference?)

該当のソースコード

using System.Collections; using System.Collections.Generic; using UnityEngine; public class werpon1 : MonoBehaviour { public GameObject AIBehavior; void Start() { float damage = AIBehavior.GetComponent<AIBehaviors>().health; } }
//AIBehaviorのスクリプト using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; #if UNITY_EDITOR using System.Reflection; #endif using Random = UnityEngine.Random; namespace AIBehavior { [RequireComponent(typeof(AIAnimationStates))] public class AIBehaviors : AIComponent { public bool isActive { get; private set; } public bool isDefending = false; [SerializeField] protected float damageMultiplier = 1.0f; ~文字数制限のため省略~ public float health = 100.0f; public float maxHealth = 100.0f; public GameObject statesGameObject = null; // === Animation Callback Info === // public Component animationCallbackComponent = null; public string animationCallbackMethodName = ""; public AIAnimationStates animationStates; // === Callbacks === // public Action<BaseState, BaseState> onStateChanged = null; public Action<AIAnimationState> onPlayAnimation = null; public Action<Vector3, float, float> externalMove = null; // === Targetting and Rotation === // ~文字数制限のため省略~ [System.Obsolete("Use Damage instead.")] public void GotHit(float damage) { Damage(damage); } public void Damage(float damage) { GotHitState gotHitState = GetState<GotHitState>(); if ( gotHitState != null && gotHitState.CanGetHit(this) ) { float totalDamage = damage * GetDamageMultiplier (); SubtractHealthValue(totalDamage); Debug.Log ("Got " + totalDamage + " damage"); if ( gotHitState.CoolDownFinished() ) { ChangeActiveState(gotHitState); } } } public virtual void SetDamageMultiplier (float newDamageMultiplier) { damageMultiplier = newDamageMultiplier; } public virtual float GetDamageMultiplier () { return damageMultiplier; }          ~文字数制限のため省略~ public float GetHealthValue() { return health; } public void SetHealthValue(float healthAmount) { health = healthAmount; } public void AddHealthValue(float healthAmount) { health += healthAmount; } public void SubtractHealthValue(float healthAmount) { health -= healthAmount; } ~文字数制限のため省略~ }
//サンプルシーンに入っていたダメージを与えるスクリプト using UnityEngine; using AIBehavior; namespace AIBehaviorExamples { public class ExampleProjectile : MonoBehaviour { public GameObject explosionPrefab; public AttackData attackData; public float playerShootingDamage; void Awake() { Destroy(gameObject, 10.0f); } void OnTriggerEnter(Collider col) { SpawnFlames(); if (col.tag == "Player") col.SendMessage ("Damage", CalculateDamage(attackData)); else col.SendMessage ("Damage", playerShootingDamage); } void OnCollisionEnter(Collision col) { SpawnFlames(); } void SpawnFlames() { Instantiate(explosionPrefab, transform.position, Quaternion.identity); Destroy(gameObject); } float CalculateDamage(AttackData attackData) { float minDamage = attackData.damage - attackData.plusOrMinusDamage; float maxDamage = attackData.damage + attackData.plusOrMinusDamage; return Random.Range (minDamage, maxDamage); } } }

試したこと

このサイトを参考に値を変更するスクリプトを書いたのですがうまくいきませんでした。
ほかのシーンで試してみたところそれはうまくいきました。

補足情報(FW/ツールのバージョンなど)

Unity 2020 3.4f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

are you missing a using directive or an assembly reference?

using または参照設定が抜けていませんか?
とエラーメッセージが出てています。

werpon1.csに下記を追加してみてはいかがでしょう?

csharp

1 2using AIBehavior; 3

投稿2021/10/17 06:52

odataiki

総合スコア948

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Aya7

2021/10/17 07:32

ありがとうございます。エラーの詳細がよくわからなかったのでほんとに助かりました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問