• 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 jorg van immerseel · May 22, 2014 at 12:31 PM · parent-child

script for parent linked to child script

i made a project(seen in the picture) and i made a scritp that counts point for my traget. the problem is that i have to attach the script to the child in order to let it work. but by doing so the scirpt always resets my count when i click different rings of the target. does someone know a way to count al the points together? maybe an script attached to to parent that gets the points from the scripts attached to the child objects? and can someone help me writing that script?

thanks for helping Jorg Van Immerseel project view:alt text

screenshot_7.png (157.5 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 22, 2014 at 03:29 PM

This is really a duplicate of this question:

http://answers.unity3d.com/questions/710779/script-updating-problem.html

You should be able to fix it by making count static:

 private static int count;

Static variables are associated with the class, not individual instances of the class. So all your scripts will share the same variable.

Comment
Add comment · Show 2 · 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 jorg van immerseel · May 22, 2014 at 04:04 PM 0
Share

thanks but shoud i just replace the count variable in the beginnig of the script by static variable?

 using UnityEngine;
 using System.Collections;
 
 public class Count : $$anonymous$$onoBehaviour
 {
     public GUIText countText;
     private int count;
     private bool _hasBeenClicked = false;
     
     public void Start()
     {
         count = 0;
         SetCountText ();
         if(gameObject.GetComponent<BoxCollider> () == null)
             gameObject.AddComponent<BoxCollider> ();
     }
     
     public void On$$anonymous$$ouseUp() {
         _hasBeenClicked = true;
         switch (gameObject.tag) {
         case "White":
             count += 1;
             break;
         case "Black":
             count += 2;
             break;
         case "Blue":
             count += 3;
             break;
         case "Red":
             count += 4;
             break;
         case "Yellow":
             count += 5;
             break;
         }
         SetCountText();
     }
     
     public void OnGUI() {
         if (_hasBeenClicked)
         {
             GUI.Label (new Rect (5, 5, 400, 100), " This is " + this.name);
         }
     }
     
     void SetCountText () {
         countText.text = "Count: " + count.ToString ();
     }
 }
avatar image jorg van immerseel · May 22, 2014 at 04:08 PM 0
Share

i already found out :) thanks mate now my thesis for school finaly works :) you are my hero :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer


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