question about properties lesson on the official site
after writing public variable and making properties for it in a class called (FirstClass) :
public int example = 5;
public int Example
{
get
{
return example;
}
set
{
example = value;
}
}
then after that we write another class called (secondClass) how do I import the variable without changing it or giving it new value? when i simply write on second class :
void Start (){
if (example == 5) {
Debug.Log ("The value is: 5" );
}
}
it will give me error that " example " doesn't exist ..
Read an explanation of Properties in regular C# sites. Unity uses C# because there are already so many ways to learn how it works. Properties are a standard C# feature. Here's my longer explanation: http://answers.unity3d.com/questions/1252826/is-unity-for-beginners-if-not-what-else-should-i-k.html
If you want to write back-and-forth about it, I moved this Q to the HelpRoom, so feel free to do that. People even like answering these Qs to help themselves understand things better. But if you just want to read something, "C# property examples" works fine.
Answer by JedBeryll · Oct 22, 2016 at 02:33 PM
First the "public int example = 5;" should be private otherwise there's no need for a property. Second, the secondClass does not have a variable called example, so you need a reference to the FirstClass in the second like this: FirstClass fc; and set this in the inspector or however you want. Then in the second class you can say if (fc.Example == 5)
I tried this , but I need the variable to be public because i need to set it on the inspector, and if I set it to private i get this error :
example is inaccessible due to its protection level
and if i make it public i get this error :
example is never assigned to, and will always have its default value 'null'
this error comes because I didn't set it on secondClass I think, so how do I import the value?
you can do [SerializeField] private int example = 5; Please post the entire scripts because by what you show here, this error message is not possible: "example is never assigned to, and will always have its default value 'null'". The other one: "example is inaccessible due to its protection level" means that you have not changed the example to Example where you tried to access it.
@JedBeryll here is the scripts :
using UnityEngine;
using System.Collections;
public class SorceScript : $$anonymous$$onoBehaviour {
private int example = 5;
public int Example
{
get
{
return example;
}
set
{
example = value;
}
}
}
second script :
using UnityEngine;
using System.Collections;
public class ImportingTheSource : $$anonymous$$onoBehaviour {
public SorceScript source;
void Update () {
if (source.Example == 5) {
Debug.Log ("success !");
}
}
}
I don't see any errors in the code. Is it still giving you errors?
Your answer
Follow this Question
Related Questions
Unity won't load inspector. Please help! 0 Answers
Unity tilemap with one point perspective 0 Answers
2D Top down player won't move down 2 Answers
Look rotation viewing vector is zero & diagonal boosting 0 Answers
Unknown loading when creating terrain 0 Answers