• 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 Drake088 · Feb 27, 2014 at 04:44 PM · other

How do I call a variable from ScriptA into a function in scriptB?

Hi all,

I'm a having a bit of an issue calling a variable from ScriptA below into ScriptB to modify the score and display it on the screen.

I'm used to javascript but thought I'd try this in C# and now I'm a bit stuck.

all of the prefabs are children of a GameObject called Drops and have the PointValues script attached as a component.

I hope this is clear enough for you guys to help me out. I would appreciate any help you can provide. Thanks!

Please be specific in your answer as I've tried many of the answers to similar questions on this site and had no luck.

This is what I tried but couldn't get to work.

MyAttempt at ScriptB (which Failed)

 using UnityEngine;
 using System.Collections;
 
 public class DestroyObjectAddPoint : MonoBehaviour 
 {
 
     int myScore = 0;
 
     public TextMesh scoretext;
 
     void OnTriggerEnter2D (Collider2D collisionObject)
     {
         var pointComponent : PointValues;
         poinyComponent = collisionObject.gameObject.GetComponent("PointValues");
         Destroy (collisionObject.gameObject);
         myScore = myScore + pointComponent.points;
 
         //Debug.Log ("My score is: " + myScore + " point(s)");
 
         scoretext.text = "Score: " + myScore;
     }
 }


Here are the scripts.

ScriptA - Point Values


 using UnityEngine;
 using System.Collections;
 
 public class PointValues : MonoBehaviour 
 {
     public int points = 0;
 
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }


ScriptB DestroyObjectsAddPoints


 using UnityEngine;
 using System.Collections;
 
 public class DestroyObjectsAddPoints : MonoBehaviour 
 {
 
     int myScore = 0;
 
     public TextMesh scoretext;
 
     void OnTriggerEnter2D (Collider2D collisionObject)
     {
 
         Destroy (collisionObject.gameObject);
         myScore++;
 
         //Debug.Log ("My score is: " + myScore + " point(s)");
 
         scoretext.text = "Score: " + myScore;
     }
 }


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
Best Answer

Answer by perchik · Feb 27, 2014 at 05:12 PM

a few things:

  • line 14, poinyComponent = collisionObject.gameObject.GetComponent("PointValues");

you have a typo. it should be pointComponent

  • line 15, Destroy (collisionObject.gameObject);

If I recall correctly, when you destroy the gameobject, the component goes with it.

rearranging your code and the typo looks like it should work:

 var pointComponent : PointValues;
 pointComponent = collisionObject.gameObject.GetComponent("PointValues");
 myScore = myScore + pointComponent.points;
 Destroy (collisionObject.gameObject);
  
 Debug.Log ("My score is: " + myScore + " point(s)");
  
 scoretext.text = "Score: " + myScore;


You might also add an if statement to see if pointComponent is null, to see if it's actually getting the right component

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 Drake088 · Feb 27, 2014 at 06:39 PM 0
Share

Perchik,

Thanks for replying.

I tried the above solution, but it's giving me one of the same errors I came across before.

on line 14

 var pointComponent : PointValues;

it does not like the ":", says unexpected symbol':', expecting ',', ';', or '=' (CS1525)

**The typo was not in the actual script that was my fault when I was retyping it on here, but thanks for the catch.

avatar image perchik · Feb 27, 2014 at 06:53 PM 0
Share

Oh oh. I missed that you were doing in this C#, I read it as Javascript.... a lot of your code is written as JS not C#.

First off, in C# you don't do var name : type; you just do type name;, so var pointComponent : PointValues; should actually be PointValues pointComponent;

Second, in C# you have to cast the result of GetComponent to make the right type, something like this :

   pointComponent = collisionObject.gameObject.GetComponent("PointValues") as PointValues;
avatar image Drake088 · Feb 27, 2014 at 07:53 PM 0
Share

perchik,

Thanks so much that worked.

Yeah I couldn't figure out the syntax for C# for that. Nut now thanks to you I've learned something extremely valuable.

Thank You!

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

21 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 avatar image

Related Questions

How do I add/subtract variables between two different JavaScripts? 1 Answer

Can't get var from other script. 1 Answer

Problem with accessing variable form other scripts. 1 Answer

Disable script on other object 2 Answers

Access other script from Editor script 2 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