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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by schult777 · Mar 22, 2014 at 10:34 PM · scoreaccess

having a hadtime with script access

I am having a impossible time with this. I have a laser script that I have on three weapons that are all attached to the player. each weapon is a child of the parent. What I want them to do is to be able to access a script that keeps score. However I am not able to do that. I have tried to find tutorials online but they are impossible to understand. Please help. I have included both scripts below. The first is the laser script and the second is the score script.

 using UnityEngine;
 using System.Collections;
 
 public class LaserbeamTrio : MonoBehaviour 
 {
     MonoBehaviour score;
     LineRenderer line;
     public GameObject explosionA;
     public AudioClip boom;
     public GameObject explosionB;
     public GameObject explosionC;
     
     void Start () 
     {    
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
     void Update () 
     {
         if(Input.GetButtonDown("Fire1"))
             
         {
             StopCoroutine("FireLaser");
             StartCoroutine("FireLaser", 0.25F);
             audio.Play();
                     }
     }
     IEnumerator FireLaser(float lengthOfTime)
     {
         line.enabled = true;
         var t = Time.time;
         
         while(Input.GetButton("Fire1") && Time.time - t < lengthOfTime)
             
         {
             Ray ray = new Ray(transform.position, transform.forward);
             RaycastHit hit;
             
             line.SetPosition(0, ray.origin);
             
             if(Physics.Raycast(ray, out hit, 150))
             {
                 line.SetPosition(1, hit.point);
                     if  (hit.collider.tag == "Asteroid")
                 {
                     Instantiate(explosionA, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                     GetComponent("Score");
                     
                 }
                     else if  (hit.collider.tag == "Missile")
                 {
                     Instantiate(explosionB, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                     GetComponent("Score");
                 }
                 else if  (hit.collider.tag == "Ufo")
                 {
                     Instantiate(explosionC, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                     GetComponent("Score");
                 }
                             }
                     else 
                 line.SetPosition(1, ray.GetPoint(150));
             
             yield return null;
         }
         line.enabled = false;
 
     }
         }


Score script

 using UnityEngine;
 using System.Collections;
 
 public class Scorer : MonoBehaviour 
 {
     private int count;
     public GUIText countText;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         count =count +1;
         SetCountText ();
 
         }
         void SetCountText ()
     {
         countText.text = "Score : " + count.ToString();
         if(count >= 100)
         {
             Application.LoadLevel ("win level 1");
         }
     }
     
 }
 
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 getyour411 · Mar 22, 2014 at 10:38 PM

Change

 MonoBehaviour score;
 to 
 public Score score;

in the Inspector, drag/drop the gameObject that has Score.cs attached to it in the slot. Once you've done that, you can reference all the variables and methods (public) in Score via score.myVar or score.count++, etc. So you probably don't want count in Update(), instead make it public and just increment it from the top script, or create a new method in bottom script like

 public void IncrementCount(int _value)
 {
 count += _value;
 }

and in your top script call score.IncrementCount(1)

Comment
Add comment · Show 3 · 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 schult777 · Mar 23, 2014 at 01:26 AM 0
Share

Thank you, I'm going to try that.

avatar image schult777 · Mar 23, 2014 at 02:21 AM 0
Share

Changed around $$anonymous$$onoBehaviour score; to public Score score; but got the error Assets/Standard Assets/Scripts/personal/LaserbeamTrio.cs(6,16): error CS0246: The type or namespace name Score' could not be found. Are you missing a using directive or an assembly reference? I also got the error Assets/Score.cs(6,21): error CS0501: Score.IncrementCount(int)' must have a body because it is not marked abstract, extern, or partial How do I fix these errors?

avatar image getyour411 · Mar 23, 2014 at 02:45 AM 0
Share

Just noticed your Score.cs is Scorer so pretty much anywhere I said Score change it to Scorer (and score to scorer, that second error you posted is because it's in uppercase)

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

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 get sound to play on certain condition (javascript) 2 Answers

How to update scoreline on a HIT? 1 Answer

Saving variables? 3 Answers

Can't edit built-in array through script unless viewed in Inspector 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