Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ngnclht · Jul 28, 2014 at 03:58 PM · property

How to update a property of Behaviour script?

I have a problem below!

I have a MarkBehaviour class that contain a property called markCount . Depend on markCount property, I will select the sprite on Update() function. At begin, I set markCount property to 0.

But when I call the setMark() function from other behaviour class, I have logged the markCount property in 2 function below, but the markCount property on Update() function was not changed, it still = 0 as the first time I set. It was changed only in setMark() function.

     public class MarkBehaviour : MonoBehaviour {
             public int markCount ;
             void Start(){
                    markCount = 0;
             }
             void Update () {    
                 Debug.Log ("mark manual setted is "+markCount);
                 int cal = calculate (markCount);
                 gameObject.GetComponent<SpriteRenderer>().sprite =numberSpriteArray[cal];
 
             }
 
             public void setMark(int mark){
                 Debug.Log ("manual set mark from other class, set "+markCount );
                 markCount = mark;
             }
     }

How can I changed it, please give a solution. Thank you very much!

EDIT

Code of other class that call setMark() function

 public class GameoverBehaviour : MonoBehaviour {
     
     public GameObject mark;
     // Use this for initialization
     void Start () {
         
         int markCount = getMark ();

         Instantiate (mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation);
         MarkBehaviour mBS = (MarkBehaviour) mark.GetComponent<MarkBehaviour>();
         mBS.setMark (markCount);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
    // for example set it to 30
     int getMark(){
        return 30;
     }
 }
Comment
Add comment · Show 5
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 meat5000 ♦ · Jul 28, 2014 at 03:57 PM 0
Share

Is the sprite renderer on the same object as the $$anonymous$$arkBehaviour class?

avatar image ngnclht · Jul 28, 2014 at 04:09 PM 0
Share

Yes, in other class, I have an GameObject that link to $$anonymous$$arkBehaviour class. From other class, I called set$$anonymous$$ark() function successfully!

avatar image ngnclht · Jul 28, 2014 at 04:10 PM 0
Share

But the markCount property still not changed in Update() function of class $$anonymous$$arkBehaviour, how can I do that?

avatar image robertbu · Jul 28, 2014 at 04:27 PM 0
Share

I see nothing wrong in this code. First are you sure you only have one game object with the above script? Second can you update the above code to include the whole class so we can verify any outer used of markCount? Also make markCount private.

avatar image ngnclht · Jul 28, 2014 at 04:36 PM 0
Share

I have added code of the class that call set$$anonymous$$ark function. Please see and give some solution, thanks!!

3 Replies

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

Answer by ngnclht · Jul 29, 2014 at 04:39 PM

Problem

When you run this line of code:

 MarkBehaviour mBS = (MarkBehaviour) mark.GetComponent<MarkBehaviour>();

You are actually saying, find the MarkBehaviour in the prefab mark. This won't work because prefab has not been instantiated, only a clone of the prefab has been.

You have two good ways of doing this.

FunctionR's Solution 1

You can Instantiate the object then SendMessage:

 GameObject g = Instantiate(mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation) as GameObject;
 
 g.SendMessage("setMark", markCount);

Notice I am keeping a reference g to the instantiated object so that I can use SendMessage on it.

FunctionR's Solution 2

You can store a reference to the Component MarkBehaviour attached to the GameObject.

 private MarkBehaviour markObj;
 
 GameObject g = Instantiate(mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation) as GameObject;
 Once you have the reference to the GameObject g you can then use the GetComponent() function.
 
 markObj = g.GetComponent<MarkBehaviour>();
 markObj.setMark(markCount);

That is solution from mr FunctionR.

Comment
Add comment · 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
-1

Answer by MaxOR PrimE · Jul 28, 2014 at 05:28 PM

to change property/variable value from a other script set a tag on the gameobject with you GameoverBehaviour script

create the new script and link it on the game object of your choice

 public class MyNewSCript : MonoBehaviour {
     
     public GameoverBehaviour scriptLink;
     
     void Awake () {
         scriptLink = GameObject.FindGameObjectWithTag("myTag").GetComponent<GameoverBehaviour>();
     }
 
     void MyFunction() {
         gameoverBehaviour.markCount = 255;
     }
 }
Comment
Add comment · 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
0

Answer by flaviusxvii · Jul 28, 2014 at 05:15 PM

markCount = 0; in Start() is happening AFTER you call mBS.setMark (markCount); Start() isn't getting called as soon as you think it is. I've run into this problem as well on a few occasions.

Get rid of markCount = 0 (you can initialize the actual variable to 0 if you like).

Comment
Add comment · 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

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

Material doesn't have a color property '_Color' 4 Answers

How to create Prefab with property 2 Answers

Using SerializedObject in a build 1 Answer

how do i get property by name out of an object? 1 Answer

Is it possible to detect a property change at runtime? 3 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