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 SVER · Apr 23, 2014 at 02:18 AM · function updateassign-variable

Issues using GetComponent

I'm have a boolean variable called 'shieldOn'. I'm trying to access a function that sets the boolean to false using GetComponent from another script and for the life of me I can't.

I get this error

UnityException: You are not allowed to call this function when declaring a

variable. Move it to the line after without a variable declaration. I pretty much copied the example right from the api reference to GetComponent and have no clue why it's doing this. Here's the link: [API: GameObject.GetComponent][1] Here's my code for scriptShield function Update () { if(shieldStrength <= 0) { Destroy(gameObject); //The line below works fine but I want to access the function instead // scriptPlayer.shieldOn = false;
//Line below is almost identical example that is given by unity var other : scriptPlayer = gameObject.GetComponent(scriptPlayer); other.SetShieldFalse(); }

} Here's the function in for the shield in the script 'scriptPlayer' static var shieldOn : boolean = false;

function SetShieldFalse() { shieldOn = false; } It works perfectly if I set the variable 'shieldOn' to static and access it from the scriptShield but I want to learn how to use the GetComponent function properlly because I'll need it when I want to access other functions that control non static variables... Help is greatly appreciated. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
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 · Apr 23, 2014 at 02:18 AM

Don't forget the parens after the GetComponent

http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

Search UA/Google for many other examples, tuts/vids, code snippets, etc on using GetComponent, it's the most frequently asked question.

Comment
Add comment · Show 2 · 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 SVER · Apr 23, 2014 at 04:41 PM 0
Share

ok, well I tried looking up some tuts but a lot are out dated... regardless I did try and follow the link you gave me and used the java script provided in it.

I'm now getting this error code:

NullReferenceException: Object reference not set to an instance of an object scriptShield.Update ()

These are the changes I have made.

 #pragma strict
 //Shield script
 
 var shieldStrength : int = 3;
 
 var playerScript : scriptPlayer;
 
 
 function Update () 
 {
     if(shieldStrength <= 0)
     {
         Destroy(gameObject);
 
         playerScript = GetComponent(scriptPlayer);
         playerScript.SetShieldFalse();
     }
 
 }

at the line with "playerScript.SetShieldFalse();" is where I get this error... Do I have to create a new empty gameobject then set it to a prefabe and drag it onto the variable in the inspector?

avatar image getyour411 · Apr 23, 2014 at 08:30 PM 0
Share

Can you confirm on your player you have a script attached named scriptPlayer? This script has a (public) field defined as playerScript, which means in the Inspector for the GameObject this script is attached to there will be a field for that. Drop and drop your player into that field which will affix the references.

Remove line 15 as you do not need to call GetComponent since you used the drag/drop method. If ins$$anonymous$$d you wanted to learn GetComponent and not use the drag/drop method, I think you are still missing parens around 15 at the end, but I don't work in JS so the syntax might be different.

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

reference an object without pre-assigning. 2 Answers

Setting Quaternion , are Euler angles updated in the same update function? 2 Answers

Endless runner, road segments spawn speed 0 Answers

my object instantiates to much 1 Answer

Is it possible to delete member variable? C# 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