Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by amer_jib · Oct 22, 2016 at 02:16 PM · unity 5properties

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 ..

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Oct 22, 2016 at 03:38 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amer_jib · Oct 22, 2016 at 02:56 PM 0
Share

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?

avatar image JedBeryll · Oct 22, 2016 at 04:41 PM 0
Share

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.

avatar image amer_jib JedBeryll · Oct 23, 2016 at 05:08 PM 0
Share

@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 !");
         }
     }
 }
avatar image JedBeryll amer_jib · Oct 23, 2016 at 05:20 PM 0
Share

I don't see any errors in the code. Is it still giving you errors?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges