Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active December 30, 2020 22:14
Show Gist options
  • Save hidsh/167b1b3061d25c02336e0ca3f35d1691 to your computer and use it in GitHub Desktop.
Save hidsh/167b1b3061d25c02336e0ca3f35d1691 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
Transform t;
// Start is called before the first frame update
void Start()
{
t = transform; // transform はクラスなので t には transform への参照がコピーされる
}
// Update is called once per frame
void Update()
{
t.position += Vector3.right; // 参照を変更しているのでコピー元に反映される → 動く
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
Vector3 v;
// Start is called before the first frame update
void Start()
{
v = transform.position; // position は構造体なので v には position の中身の値がコピーされる
}
// Update is called once per frame
void Update()
{
v += Vector3.right; // vを変更して、positionが変更されることを期待しているがそうならない
// position は値型でコピーされているので v が変更されるだけで
// コピー元の position には反映されない → 動かない!
}
}
@hidsh
Copy link
Author

hidsh commented Dec 30, 2020

C# の値型と参照型

値型

int
double
float
char
bool
構造体 <--!!!

参照型

string
class <--!!!
配列

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment