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

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

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

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

Unity

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

Q&A

解決済

1回答

4000閲覧

Unityを用いたカレンダー作成について

Yoshiki.

総合スコア13

C#

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

Unity

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

0グッド

1クリップ

投稿2017/06/02 08:27

###前提・実現したいこと
Unityを使用しカレンダーの作成を行っています。
当方初心者のためこちらのサイト"http://tiwaluna.hatenablog.com/entry/2017/02/18/000515"を参考に進めております。
こちらのサイトに載っているソースコードをそのままC#でプログラミングしている途中です。
サイトの通りに進めているのですが以下のようなエラーが出てしまいお手上げ状態です。
VisualStudioで正常にビルドはできます。

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

エラーメッセージ

IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
CalendarManager.InitCalendarComponent () (at Assets/CalendarManager.cs:62)
CalendarManager.Start () (at Assets/CalendarManager.cs:30)

###該当のソースコード

ここにご自身が実行したソースコードを書いてください

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UniRx;

public class CalendarManager : MonoBehaviour
{

///<summaryボタンの親オブジェクト</summary> public GameObject calenderParent; /// <summary>来月へ</summary> public Button nextButton; /// <summary>先月へ</summary> public Button prevButton; /// <summary>カレンダーの日時</summary> public DateTime current; /// <summary>Buttonオブジェクト</summary> GameObject[] objDays = new GameObject[42]; /// <summary>カレンダーの日付マス</summary> CalendarButton[] Days = new CalendarButton[42]; // Use this for initialization void Start() { current = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day); InitCalendarComponent(); SetCalendar(); if (nextButton != null) { //押されたら起動 nextButton.onClick.AsObservable() .Subscribe(_ => { //一つ月を進める current = current.AddMonths(1); SetCalendar(); }); } if (prevButton != null) { prevButton.onClick.AsObservable() .Subscribe(_ => { current = current.AddMonths(-1); SetCalendar(); }); } } /// <summary>コンポーネントの取得、設定</summary> void InitCalendarComponent() { //行 for (int i = 0; i < calenderParent.transform.childCount; i++) { //子オブジェクトを保存 objDays[i] = calenderParent.transform.GetChild(i).gameObject; //コンポーネントを設定、取得 Days[i] = objDays[i].AddComponent<CalendarButton>(); Days[i].index = i + 1; } } /// <summary>カレンダーに日付をセット</summary> void SetCalendar() { int day = 1; //今月の1日目 var first = new DateTime(current.Year, current.Month, day); //来月 var nextMonth = current.AddMonths(1); int nextMonthDay = 1; //先月 var prevMonth = current.AddMonths(-1); //先月の場合は後ろから数える。 int prevMonthDay = DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month) - (int)first.DayOfWeek + 1; foreach (var cDay in Days) { //今月の1日より前のマスには先月の日にちを入れる。 if (cDay.index <= (int)first.DayOfWeek) { cDay.dateValue = new DateTime(prevMonth.Year, prevMonth.Month, prevMonthDay); prevMonthDay++; } //今月の最終日より後ろのマスには来月の日にちを入れる。 else if (day > DateTime.DaysInMonth(current.Year, current.Month)) { cDay.dateValue = new DateTime(nextMonth.Year, nextMonth.Month, nextMonthDay); nextMonthDay++; } //今月の日にちをマスに入れる。 else { cDay.dateValue = new DateTime(current.Year, current.Month, day); day++; } } }

}
###試したこと
課題に対してアプローチしたことを記載してください

###補足情報(言語/FW/ツール等のバージョンなど)
Windows8.1
Unity 5.6.1
VisualStudio2017

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

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

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

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

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

guest

回答1

0

ベストアンサー

「配列のIDが存在しない(配列のサイズから指定IDが外れている)」というエラーです。

void InitCalendarComponent()内で
calenderParent.transform.childCountの数だけforを回していますが、
恐らくobjDaysかDaysの数(42)をオーバーしているのだと思います。

ヒエラルキーのオブジェクトを確認し、
calenderParentの子オブジェクトの個数が正常か確認してみてください。

投稿2017/06/02 10:56

sakura_hana

総合スコア11427

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問