- Home /
Share Public Classes
So I want to create a custom public class, such as:
public class Info {
public string Name;
}
but I need to know how to use this class on another script, so that I can share the info in a simple manner.
public Info MyInfo = new Info();
MyInfo.Name="Adam";
BattleOrganizer BattleScript = Camera.main.GetComponent<BattleOrganizer>();
BattleScript.AddPlayer(MyInfo);
Can anyone help me with this? If you need more info let me know.
I am confused, this should be fine. What is it that is not working?
Side note: I would recommend na$$anonymous$$g your variable with lower can letter as in battleScript.
This is the "standard" na$$anonymous$$g convention.
You can use mBattleScript as well. There is also Hungarian notation ( int i$$anonymous$$yInt, string sName), up to you
The purpose of this is that Classes and Functions should begin with capitals.
It makes it much easier to read, since without looking at anything but that name you can tell that BattleOrganizer is a class, mBattleScript is a variable.
When I send the class to the second script i get this error: Assets/PlayerBattleScript.cs(14,16): error CS0246: The type or namespace name `Info' could not be found. Are you missing a using directive or an assembly reference?
In other words the class Info is not available on my battleScript (which I shall rename), but only on the script PlayerInfo
Wait, is this a class set up like:
public class PlayerInfo:$$anonymous$$onobehavior
{
public class Info
{
public string mName;
}
public Info myInfo;
void Awake()
{
Info myInfo = new Info();
myInfo.Name="Adam";
BattleOrganizer mBattleScript = Camera.main.GetComponent<BattleOrganizer>();
mBattleScript.AddPlayer($$anonymous$$yInfo);
}
}
Answer by Avaista · Jul 16, 2012 at 02:10 AM
If it is, you need to use PlayerInfo .Info as the type
as in
public class BattleOrganizer:Monobehavior
{
public void AddPlayer(PlayerInfo.Info newInfo)
{
//DO STUFF
}
}
Your answer
Follow this Question
Related Questions
Magic Spell Types - SpellType.Fire/Water/Earth? 1 Answer
Modify a variable inside a game object from other object. 1 Answer
transform in a custom class 2 Answers
an object reference is required to access non static member problem 0 Answers
How to pass an instance of a custom class into an event within itself? 2 Answers