Unity how to access components in a class [duplicate] - c#

This question already has answers here:
Access variables/functions from another Component
(4 answers)
Closed 5 years ago.
I have made a character class with many variables, I made an RPG long ago in which I had a method to equip gear and unequip it, adding and subtracting values, pretty simple, I would:
public void equip(Gear g){
increaseValues(g.getValue());
//increaseValues..
}
public void unequip(Gear g){
increaseValues(-g.getValue());
//increaseValues..
}
Pretty basic, I could do the same in Unity but here is where the problem comes, in OOP I would create a class with values and methods but the trend in unity is to create scripts and add them as components to another class, thus I have no idea how to access the information on an object embedded component, what do I pass in the parameters? I tried GameObject but it tells me that the values or methods I'm trying to access do not exist within such object.

You can access other GameObject's scripts via GameObject.GetComponent(Type type) method.
public GameObject myGameObject; //Attach gameobject in Unity Editor
private MyClass myGameObjectClass;
void Start() {
myGameObjectClass = myGameObject.GetComponent<MyClass>();
//or you could use this
//myGameObjectClass = myGameObject.GetComponent(typeof(MyClass)) as MyClass;
myGameObjectClass.MyProperty = 2;
myGameObjectClass.MyMethod();
}

Related

Serializable field in Unity [duplicate]

This question already has answers here:
Why should I use SerializeField?
(2 answers)
Closed 2 years ago.
I have a little question, if someone can help me, I'll be grateful.
What is Serializable field in Unity ?
How it works for an object of a class ?
In code it's looks like :
[Serializable]
or
[System.Serializable]
[Serializable]
private GameObject gameobject;
[System.Serializable]
private int timer;
The serializable field works like a public variable except that you can using it for a private variable. Some people just use a public variable but a serializable private variable works the same.
Might be a lazy answer... but Unity provides a very useful documentation (with examples) which explains every command, even [Serializable].
Here is the link to what you are looking for:
https://docs.unity3d.com/ScriptReference/Serializable.html

How do i initialise a property on a class in C# 6 [duplicate]

This question already has answers here:
What is the difference between getter-only auto properties and expression body properties?
(2 answers)
Closed 7 years ago.
I have a class that uses some services. I need each service to be instantiated on creation of the class. In C# 6 I can see 2 ways of doing this but I'm not sure which would be correct...
protected static SomeServiceType Service => new SomeServiceType();
alternatively I could use an autoproperty initialiser...
protected static SomeServiceType Service { get;} = new SomeServiceType();
What are the advantages/drawbacks with each approach?
Many thanks
I believe
the former ("Expression-bodied members") calls new SomeServiceType() every time the property is read
the latter ("Auto-property initializers") calls it once on instantiation, and returns the created instance every time the property is read.
It sounds like you want the latter.

What is the difference between a public and private variable [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
Newbie Incoming, coming with a question about Unity, and C# in particular. This could be really simple, but I could not figure it out.
What's the difference between public and private mean? I don't get it. public, private, it boggles my mind. I just can't get it through my head, which doesn't happen very often. Can someone explain it to me like I'm five? It would really help me out on my journey of making a ball move across the ground. Thank you in advance.
Good night .
The difference is :
Private : Those variables or " functions" which may only be used in the source class .
Public : Those variables or "functions" that can be used in various class .
Classes: A class is a construct that allows create your own custom types by grouping variables of other types , methods and events. A class is like a blueprint . Defines the data and behavior of a type.

Variable read only [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a problem with changing a variable from different class. Even tho I used access modifiers (get and set), compiler still sees it as read only, and I cannot change it within another class.
private float speed;
public float Speed
{
get {return speed;}
set {speed = value;}
}
I'm not sure if this matters, but this variable is from my main, abstract class and I use it in other classes. What is happening here, is that I assigned value to this variable in one class (Player.cs) and I'm trying to change it in another class by initializing its object (Physics.cs). They are both inheriting from that main, abstract class.
If you're only setting speed once, just use a readonly variable and set it in the constructor:
public class Physics
{
private readonly float speed;
public Physics()
{
this.speed = 5;
}
}
You can set a readonly variable exactly once.
If you really do need to change speed within the lifetime of your object, just use an auto property anyways for simplicity:
public float Speed {get; set;}
If you're trying to access speed from a child class, make sure that its defined as protected (not private) in the base class. private really is private. Nothing outside of the class where it is defined can access it. Protected allows classes that inherit to access it.
(If I understood your question correctly, that is. I'm confused, though... if you are trying to access the speed variable directly, then why have the public properties?)

Does it make any practical difference to instantiate an object inside the constructor? [duplicate]

This question already has answers here:
What is the difference between instantiation in constructor or in field definition?
(5 answers)
Initialize class fields in constructor or at declaration?
(16 answers)
Closed 8 years ago.
Is there any practical difference between those two ways of instantiating an object?
public class myClass
{
private myType myObject = new myType();
}
and
public class myClass
{
private myType myObject;
public myClass()
{
myObject = new myType();
}
}
Thanks for helping.
No there isn't any practical difference, between the two ways you provided. They are exaclty the same.
The answer is yes
When you read the code later, you will look in the constructor to see what happens when you create the class. If you put constructor logic outside the constructor another developer may miss what's going on. So put your constructor logic in your constructor. It makes a difference.
As far as the code goes, there isn't much difference. Initialization in the declaration happens in document order, top to bottom, which might or might not have side effects. From a practical perspective, if you do any interactive debugging at all, you'll grow to hate declaration initializations, unless you enjoy stepping through them one at a time.
Keep your code tidy and initialize things in the constructors.

Categories

Resources