みかづきブログ その3

本ブログは更新を終了しました。通算140万ユーザーの方に観覧頂くことができました。長い間、ありがとうございました。

👆

引越し先はこちらです!

The type or namespace name `Text' could not be found. Are you missing `Unity Engine.UI' using derective?

Unityをはじめてみて最初にはまったことを忘れないようにメモしておきますが、

テキストを取得しようとして、

this.msgTxt.GetComponent<Text> ().text = "Ya-Ha-!";

みたいなコードを書いたら、

The type or namespace name `Text' could not be found. Are you missing `Unity Engine.UI' using derective?

というエラーがでて、

this.msgTxtが取得できてないのか?

とか、

GetComponentを使うんじゃないのか?

とか、

じゃないのか?

とか、いろいろ考えたんですが、原因はエラー文言を読んで字のごとく、

using UnityEngine.UI;

を忘れていただけでした。。。

エラー文言はちゃんと読もうと思いました。

Can not fetch: とか 言われて nodebrew で node のインストールが失敗する

nodebrew install-binary 8.9.4

でインストールしようと思ったら、

Can not fetch: https://nodejs.org/dist/v8.9.4/node-v8.9.4-darwin-x64.tar.gz

という感じで失敗しました。

かつてnodebrewをインストールしたとき のように、

curl -L git.io/nodebrew | perl - setup

と再度nodebrewをいれたら解決しました。

kimizuka.hatenablog.com

噂によると、

nodebrew selfupdate

でも解決するらしいです。

続・ランダムに数字を抽選しつつ、一巡するまでは同じ数字を抽選しない(非復元抽出)

kimizuka.hatenablog.com

以前、一度つくった事があるんですが、Node.js用に書き換えました。
また、「ランダムに数字を抽選しつつ、一巡するまでは同じ数字を抽選しない」は「非復元抽出」ということも親切な方に教えていただきました。

Lottery.js

"use strict";

class Lottery {
  constructor(length) {
    this._length = length;
    this._pod = [];

    this.reset();
  }

  reset(opt_length = this._length) {
    this._pod = [];

    for (let i = 0; i < opt_length; ++i) {
      this._pod.push(i);
    }
  }

  select() {
    let num = this._pod.splice(Math.random() * this._pod.length | 0, 1)[0];

    if (!this._pod.length) {
      this.reset(this._length); // 空になったらリセット
    }

    return num;
  }
}

module.exports = Lottery;

つかいかた

"use strict";

const Lottery = require("./Lottery");

let lottery = new Lottery(12); // 0 ~ 11までの中で抽選

for (let i = 0; i < 24; ++i) { // 試しに24回抽選してみる
  console.log(lottery.select());
}


こんな感じです。
非復元抽出は、非復元だけに、空になった後も復元しないものらしいのですが、
個人的には空になったらリセットする形を求められることが多いので、そちらを前提に作っています。
本当は空になった時はEventEmitterとかで通知して、ユーザーに処理を任せるほうが良いのかもしれません。