• 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 joseph b · May 09, 2011 at 05:53 PM · javascriptgetcomponent

error on script getcompnent function

i have to scripts and the first one neads to get a variable value from the second however although the value i want is public it dosent get it it comes up null if i print it and i dont have a clue why

public var curwp : Transform; public var open : Transform[]; var bestrate : float = 10000; var val : Transform;

function Start () { open = GetComponentsInChildren.<Transform>(); }

function Update () { bestrate = 10000; for (var val : Transform in open){ print ("val =" + val); var ratesorce = val.gameObject.GetComponents (score); <---- this print (ratesorce); print (ratesorce.tlscr); if (ratesorce.tlscr < bestrate){ bestrate = ratesorce.tlscr; print (bestrate); } } }

second script

var trg: Transform; var discr : float; var mvcst : float; public var tlscr : float; <---conects to this... or shold var cont : GameObject;

function Update () { curwps = cont.GetComponent (controler); curwpo = curwps.curwp; discr = Vector3.Distance(trg.position, transform.position);

if (Vector3.Distance(curwpo.position, transform.position) <= 2.9){ mvcst = 30; transform.renderer.material.color = Color.yellow; if (Vector3.Distance(curwpo.position, transform.position) <= 2){ mvcst -= 10; transform.renderer.material.color = Color.green; } } else { transform.renderer.material.color = Color.red; mvcst = 10000; }
tlscr = mvcst + discr; <---- dose this have anything to do with it }

any help is aprichiated

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 Bunny83 · May 09, 2011 at 06:05 PM

It's the good old casting problem. GetComponent returns a reference of type Component. You either have to cast it explicit or implicit into the right type.

// the generic version
var ratesorce = val.GetComponents.<score>();
// or implicit casted
var ratesorce : score = val.GetComponents (score);
// or explicit casted
var ratesorce = val.GetComponents (score) as score;

Now ratesorce is of type score and you can access the public members of this class.


edit

I actually don't understand why you use an array of Transform when you just want to access your score script.

That way it's faster and much more reliable. It's the straight forward way:

public var curwp : Transform; public var open : score[]; var bestrate : float = 10000;

function Start () { open = GetComponentsInChildren.<score>(); }

function Update () { bestrate = 10000; for (var val : score in open){ if (val.tlscr < bestrate){ bestrate = val.tlscr; print (bestrate); } } }

Comment
Add comment · Show 6 · 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 joseph b · May 09, 2011 at 07:01 PM 0
Share

ime afraid this dident work

avatar image Bunny83 · May 09, 2011 at 08:05 PM 0
Share

Are you sure that all of your objects have the score script attached? If you try to get the a component that isn't there you will of course get a null reference.

avatar image Bunny83 · May 09, 2011 at 08:45 PM 0
Share

I have added another solution. I guess your problem is that the GameObject that have this script attached is also returned by GetComponentsInChildren. If you run the game in the editor you should be able see all objects that have been returned in your inspector (since your open array variable is public). if you click on one element it gets highlighted in the hierarchy. I'm sure there is one object that doesn't have the score script attached. I'm pretty sure it's the one with the other script.

avatar image joseph b · May 10, 2011 at 12:52 PM 0
Share

thanks that worked and one more question can you get the transform of the scripts game object ??

avatar image Bunny83 · May 10, 2011 at 01:32 PM 0
Share

Sure, every component have all the default shorthand properties like .transform, .renderer or .camera. The only one that should always work is .transform because every gameboject have a Transform component. All others can return null when there is no component of that type.

Show more comments

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

No one has followed this question yet.

Related Questions

Error when using GetComponent<> in a C# script, on a javascript 2 Answers

get component javascript from a c# script 3 Answers

How could you access a script of varying name? 5 Answers

Scripts not functioning or interacting. 1 Answer

Access a script that has NO class? 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