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 Raymond 2 · Feb 09, 2011 at 10:06 AM · getcomponentvariables

acces variables from other script

I'm trying to acces a variable out of an other script, and i did find something that should work, but i don't know how exactly

This is the code: var feather : Transform; var script : TrackItems; script = GetComponent("TrackItems");

function OnTriggerEnter () { Destroy(feather.gameObject); script.feathers (script.feathers -1); }

I think it's clear what i want to happen, i just don't know how to Exactly do this, anyone who can help?

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

2 Replies

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

Answer by GesterX · Feb 09, 2011 at 10:36 AM

Create an empty GameObject and call it GameState and attach your TrackItems script to it. Set the tag of the GameState object to "GameController" in the inspector.

Your feather object must have a collider set as a trigger.

Attached this script to the feather:

//get the itemTracking Script var itemTracker;

function Start() { itemTracker = GameObject.FindWithTag("GameController").GetComponent(TrackItems); }

function OnTriggerEnter () { itemTracker.feathers -= 1; //subtract 1 from feathers in TrackItems Destroy(gameObject); }

Comment
Add comment · Show 7 · 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 Raymond 2 · Feb 09, 2011 at 10:42 AM 0
Share

Tried that, but i now get this error:

NullReferenceException: Object reference not set to an instance of an object DestroyFeatherCollision.OnTriggerEnter () (at Assets/scripts/DestroyFeatherCollision.js:8)

avatar image GesterX · Feb 09, 2011 at 10:51 AM 0
Share

What is this script attached to? If it is attached to the feather then you don't need the feather var (first line) and the destroy should be called as Destroy(gameObject).

I would reccommend putting the script.feathers -= 1; above the destroy function call as well.

Also since you are using the OnTriggerEnter function make sure that one of the colliders attached to the feather has the is Trigger box ticked.

avatar image GesterX · Feb 09, 2011 at 10:58 AM 0
Share

I have edited the original answer and included instructions...

avatar image Raymond 2 · Feb 09, 2011 at 11:18 AM 0
Share

Sadly enough, still not working.. now got this errors:

NullReferenceException: Object reference not set to an instance of an object DestroyFeatherCollision.OnTriggerEnter () (at Assets/scripts/DestroyFeatherCollision.js:8)

Assets/scripts/DestroyFeatherCollision.js(6,9): BCE0005: $$anonymous$$ identifier: 'itemTracker'.

avatar image GesterX · Feb 09, 2011 at 11:20 AM 0
Share

$$anonymous$$y mistake I gave you some dodgey syntax. Add var to the start of itemTracker on the first line. I have edited the original answer to reflect this.

Show more comments
avatar image
0

Answer by Raymond 2 · Feb 09, 2011 at 11:36 AM

I got it working in an other way, here's what i have done:

//get the itemTracking Script var itemTracker; itemTracker = GameObject.FindWithTag("GameController").GetComponent(TrackItems);

function OnTriggerEnter () { itemTracker.feathers -= 1; //subtract 1 from feathers in TrackItems Destroy(gameObject); }

Comment
Add comment · Show 1 · 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 GesterX · Feb 09, 2011 at 11:39 AM 0
Share

Congratulations. Essentially I am doing the same except I am assigning the itemTracker in the Start function which would still work. If you think my answer "answered" your question then mark it with the tick. Thanks.

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

No one has followed this question yet.

Related Questions

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

How to access scripts other than by name. 1 Answer

Changing static variables from another script? 1 Answer

C# - Problem with trigger that won't activate 1 Answer

Easily cleaning up \ deleting unused variables? 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