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

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

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

1回答

1464閲覧

Unityでインスタンス化されたクローンの色を変更したい

roua

総合スコア22

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

1グッド

0クリップ

投稿2019/10/06 07:27

編集2019/10/07 02:15

前提・実現したいこと

作ったクローンの色を黄色にしたいんですが、うまくできません。どなたか原因わかる方いましたらよろしくお願いします。

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

下のコードの63行目からのインスタンス化されたクローンの色を変更できません。

該当のソースコード

C#

1using System.Collections; 2using System; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class NotesIdou : MonoBehaviour 7{ 8 [SerializeField] private Sound anotherScript; 9 [SerializeField] private Score AnotherScript; 10 public Slider pitchSlider; 11 public static float speed; 12 13 // Update is called once per frame 14 15 public void AddScore(int y) 16 { 17 GameObject anotherObject = GameObject.Find("Oto"); 18 anotherScript = anotherObject.GetComponent<Sound>(); 19 anotherScript.Oto(); 20 GameObject AnotherObject = GameObject.Find("Score GUI"); 21 AnotherScript = AnotherObject.GetComponent<Score>(); 22 AnotherScript.AddPoint(y); 23 } 24 public void Hantei() 25 { 26 GameObject anotherObject = GameObject.Find("Oto"); 27 anotherScript = anotherObject.GetComponent<Sound>(); 28 float y; 29 var clones = GameObject.FindGameObjectsWithTag("1"); 30 foreach (var clone in clones) 31 { 32 Vector3 tmp = clone.transform.position; 33 y = tmp.y; 34 35 if (Humen1_3.pause == 0) 36 { 37 float speed2 = speed / 6.0f; 38 if (y >= -1.0f * speed2 && y <= 1.0f * speed2) 39 { 40 anotherScript.Oto(); 41 AddScore(500); 42 Destroy(clone); 43 break; 44 } 45 else if (y >= -2.0f * speed2 && y < -1.0f * speed2 || y <= 2.0f * speed2 && y > 1.0f * speed2) 46 { 47 anotherScript.Oto(); 48 AddScore(300); 49 Destroy(clone); 50 break; 51 } 52 } 53 54 55 } 56 57 58 59 } 60 61 public void Idou(int a, int b, int c, int d) 62 { 63     !!63行目 64 if (d == 1) 65 { 66 var clone = gameObject; 67 var material = clone.GetComponent<Renderer>(); 68 material.material.color = Color.yellow; 69 } 70 switch (Humen1.speed) 71 { 72 case 1: 73 a -= 1; 74 b -= 1; 75 76 if (b < 0) 77 { 78 b += 4; 79 a -= 1; 80 } 81 speed = 1f; 82 break; 83 case 2: 84 b -= 3; 85 c -= 2; 86 if (c < 0) 87 { 88 c += 4; 89 b -= 1; 90 } 91 92 if (b < 0) 93 { 94 b += 4; 95 a -= 1; 96 } 97 speed = 2f; 98 break; 99 case 3: 100 b -= 2; 101 102 if (b < 0) 103 { 104 b += 4; 105 a -= 1; 106 } 107 speed = 3f; 108 break; 109 case 4: 110 b -= 1; 111 c -= 2; 112 if (c < 0) 113 { 114 c += 4; 115 b -= 1; 116 } 117 118 if (b < 0) 119 { 120 b += 4; 121 a -= 1; 122 } 123 speed = 6f; 124 break; 125 case 5: 126 b -= 1; 127 if (b < 0) 128 { 129 b += 4; 130 a -= 1; 131 } 132 speed =12f; 133 break; 134 } 135 StartCoroutine(Idou1_2(a, b, c, d)); 136 } 137 public IEnumerator Idou1_2(int a, int b, int c, int d) 138 { 139 GameObject anotherObject = GameObject.Find("Oto"); 140 anotherScript = anotherObject.GetComponent<Sound>(); 141 Transform myTransform = this.transform; 142 Vector3 worldPos = myTransform.position; 143 yield return new WaitUntil(() => Timing(a, b, c) == true); 144 145 for (; worldPos.y > -6f;) 146 { 147 yield return new WaitUntil(() => Humen1_3.pause == 0 == true); 148 worldPos.y -= speed * 4 * Humen1.time * pitchSlider.value; 149 myTransform.position = worldPos; 150 if (worldPos.y < 0f) 151 { 152 anotherScript.Oto(); 153 AddScore(500); 154 Destroy(gameObject); 155 } 156 //} 157 } 158 Destroy(gameObject); 159 } 160 161 public bool Timing(int a, int b, int c) 162 { 163 return Music.IsJustChangedAt(a, b, c); 164 165 } 166}

試したこと

・63行目のif文の中にDebug.Logを入れ、確認しましたが動作していました。
・カラー変更について調べ、色々なサイトの色変更のスクリプトも試しましたが上手くいきませんでした。

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

音ゲーの製作中です。
このスクリプトは色を変えたいオブジェクト(降ってくるノーツ)にアタッチしてあります。
最初にそのオブジェクトが他のスクリプトによって大量にクローンされ、引数が渡されます。
その引数が1ならこのクローン(一つだけ)色を変える...というものです。

バージョン
Unity2019.2.5f1

退会済みユーザー👍を押しています

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

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

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

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

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

sakura_hana

2019/10/07 01:39

・63行目がどこなのか念の為明示してください。 ・エラーが出ているならそれを記載してください。 ・このスクリプトが何のオブジェクトに付いているか明記してください。 ・何が起こっているかを具体的に記述してください。(どのオブジェクトの色も変わらない、clones全部の色が変わってほしいのにこのオブジェクトの色だけ変わる、など) まぁこのコードだと「自身のオブジェクトを黄色にする」ということしか書かれていないので、このスクリプトが付いたオブジェクトがクローンでないのなら想定外の挙動になりますね。
guest

回答1

0

自己解決

解決しました!元々は

↑こうだったんですが、Colorを白にして、赤いマテリアルを新しく作り、それをアタッチすれば色が変わるようになりました!

投稿2019/10/07 02:32

roua

総合スコア22

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問