• 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 SomeRandomGuy · Nov 12, 2011 at 12:08 PM · getcomponent

Problem with implenting a simple point system

Hi, I'm trying to make a simple point system for a game I'm working on, However, I can't get it to work. What I basically need to do is send a notice of some sort to my point system when a point is destroyed. my points and point system are different game objects so I tried the following for my points:

 var AdjustPoints =false;
 function OnTriggerEnter (other : Collider)
     {
             AdjustPoints =true;    
             Destroy(this.gameObject);
         
     }

and this for my point system:

 var points :int = 0;
 
 function Update()
 {
     var Points = GameObject.FindWithTag("Point");
     var Adjust = (Points.GetComponent(DestroyPointScript).AdjustPoints);
     if (Adjust = true)
         {
             points + 1;
         }
 }

Two simple scripts that for as far as I can see should work, however it does not. I've tried a couple variations on this as well, however those only gave me more errors :|

This only gives me a couple small errors, which are the following:

Assets/Scripts/General/PointSystem.js(7,20): BCE0044: expecting ), found '='.

Assets/Scripts/General/PointSystem.js(7,22): BCE0043: Unexpected token: true.

Assets/Scripts/General/PointSystem.js(9,35): BCE0044: expecting :, found ';'.

I can imagine these are easily fixed, however I can't seem to find how exactly. I keep getting the famous "Accessing other GameObjects" page, however this doesn't seem to solve my problem. Or I am somehow misreadiing something there... I could use some help fixing this:D

Anyway, Thanks in advance!

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

Answer by save · Nov 12, 2011 at 12:26 PM

There are a couple of things you should keep in mind.

== (Equals equals) is the if-statements equals to. When you work with booleans you only have to do this:

if(Adjust) //If (Adjust == true)
if(!Adjust) //If (Adjust != true)

A single equals is for setting variables. Don't forget to type booleans also (all your variables should), var AdjustPoints : boolean = false;.

Isn't it better to run a function for adjusting points called from OnTriggerEnter?

function OnTriggerEnter () { AdjustPoints(); Destroy(this.gameObject); }

static function AdjustPoints () { points++; //And so on }

If you don't need the Collider then remove it from OnTriggerEnter, it's unnecessary to set the variable other if you don't use it. Also never ever run a find-function from within Update(), use functions instead that only gets called when they're needed. Don't get components inside Update() either. Use Start(), Awake() or your own function to retrieve components from other objects,

private var dps : DestroyPointScript;
private var points : GameObject;
function Start () {
     points = GameObject.FindWithTag("Point");
     dps = points.GetComponent(DestroyPointScript);
}

To make things easier in the long run disable dynamic scripting by using #pragma strict on the top of your document(s).

Comment
Add comment · Show 4 · 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 SomeRandomGuy · Nov 12, 2011 at 12:52 PM 0
Share

Hmm thats a whole new way of doing things...Let me try this out and see if it works! I have my doubts about some little things, but I'll just have to see about that. I'll post whether it worked or not in a bit!:D

avatar image SomeRandomGuy · Nov 12, 2011 at 01:12 PM 0
Share

Hmm it seems to work now, however, I am having a problem with finding my points/dps variables. its giving me a nullrefferenceexception. However I have checked and my points do have the Point tag, and the script is called DestroyPointScript. Its weird...$$anonymous$$ay this be caused by the fact that my points are being spawned in the Update function in their respective scripts?(I gave them a variable to toggle whether or not it should spawn, which if false spawns and turns true. if its true its not doing anything. Could I just put this in a start function ins$$anonymous$$d?)

avatar image SomeRandomGuy · Nov 12, 2011 at 01:31 PM 0
Share

EDIT: Hmm it seems changing those Update functions to Awake functions worked out the nullrefferenceexception, however it still isn't actually picking up the points and dps. I edited the dps and points variables so I could see them in the inspector, and they didn't get anything assigned to them.

avatar image SomeRandomGuy · Nov 12, 2011 at 02:42 PM 0
Share

I found out that even tho it does not show them in the inspector, it does still work, so thanks a lot!:D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Passing a string from Javascript to C# 1 Answer

How to Remove Multiple Components of Same Type from Game Object? 2 Answers

AddComponent not working 3 Answers

GetComponent Errors 2 Answers

Updating GUIText 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