This is a discussion topic, I just want to know what you guys think about placing multiple classes into one single file like CSscript.cs
, if it is a good practice, if it changes the performance (I believe it does not? But I am not sure), if it is organized, etc. And in which situations do you use it. If you don’t understand what I mean, here is an example:
Using two classes into one script:
People.cs
using System;
using System.Collections;
using UnityEngine;
[Serializable]
public class PersonOne
{
public PersonTwo Listener;
public string Question = "Can you hear me?";
public void Ask()
{
Debug.Log(Question);
Listener.Respond();
}
}
[Serializable]
public class PersonTwo
{
public bool CanHear;
public string Answer = "Yes!";
public void Respond()
{
if (CanHear)
{
Debug.Log(Answer);
}
}
}
Using two different scripts each with a class:
PersonOne.cs
using System;
using System.Collections;
using UnityEngine;
[Serializable]
public class PersonOne
{
public PersonTwo Listener;
public string Question = "Can you hear me?";
public void Ask()
{
Debug.Log(Question);
Listener.Respond();
}
}
PersonTwo.cs
using System;
using System.Collections;
using UnityEngine;
[Serializable]
public class PersonTwo
{
public bool CanHear;
public string Answer = "Yes!";
public void Respond()
{
if (CanHear)
{
Debug.Log(Answer);
}
}
}
Of course the answer is unlikely to be “Never.” or “Always.”, so be free to explain in what situations do you think it’s best and what do you personally like to do. I personally never use them as I think it’s more organizing to have each script with it’s own class and that is it, but I would like to hear your opinions!