突然のブログ再開!!
unityで四苦八苦してるので
公開用というか自分用メモに・・・
■他のクラスのフラグをgetcompornentできない
これで詰まって2週間ぐらい放置してた
なんかぐぐったら以下サイトがひっかかる
http://blog.be-style.jpn.com/article/53414359.html
>NullReferenceException: Object reference not set to an instance of an object
>原因は、プログラムを適用しているGameObjectに、参照先のクラス(JavaScript, C#ファイル)
>を適用していないことでした。
お( ^ω^)?
初歩的すぎると丁寧に解説してくれててもわからないんすよねぇ
くやしいーー
わかってしまえば理由は単純でした。
AddComponentっててっきりスクリプト内でのやりくりだと
思い込んでたんですが
unity上で呼び出しが必要でした。
flagManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flagManager : MonoBehaviour {
public bool flg;
}
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public bool isOpened = false;
private flagManager flagmanager;
private void Start()
{
flagmanager = GetComponent<flagManager>();
//Debug.Log(flagmanager.flg);
Debug.Log("aa");
Debug.Log(flagmanager);
flagmanager.flg = true;
Debug.Log(flagmanager.flg);
}
だめな例
よい例
2,3ヶ月おきにプログラム書くからこういうことになるのです。
ここしばらくのもやもやがすっきり
PR
- Newer : シーン共通のオブジェクト
- Older : あけまして2017
