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
-1
Question by SepM · Jul 13, 2014 at 06:55 PM · variablegetcomponentcontrolstatstwice

GetComponent Variable Referenced Twice

I'm making a fighting game that uses three main scripts per character. There's the stats, control, and projectile scripts. The stats and control both go within the character while the projectile script is in my projectile prefab. A variable (Character2) was GetComponent-ed from the stats script to the control script. It worked. When I did the same with the projectile script, it wouldn't set and stayed as zero.

Can a variable only be referenced once to before the GetComponent action is unusable? I don't want to combine the Stats and Control scripts, but I will if I need to.

UPDATE: All right. I'll try out what Tehnique is suggesting, but first, I'll paste some of my script here.

This is for Stats2:

 var Character2 = 15;

Control2 (receiving the variable works):

 var Character2 : int;
 function Update () {
 //Receive character variables.
     Character2 = GetComponent("Stats2").Character2;

Projectile2 (receiving the variable does not work):

 var Character2 : int;
 function Update () {
 //Receive character variables.
     Character2 = GetComponent("Stats2").Character2;

Comment
Add comment · Show 3
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 tanoshimi · Jul 13, 2014 at 07:02 PM 0
Share

GetComponent() returns a reference to a script - it should never return zero. Can you please post a section of the relevant code you are using.

avatar image Tehnique · Jul 13, 2014 at 07:14 PM 0
Share

I think you mean "null", not zero. Anyway, if you copy-pasted the code from control script to projectile script, it won't work. That's because in Control you can use transform.gameObject.GetComponent to access the stats script (they share the same transform). The projectile script sits on a projectile from what I understand, so you need to refer the character in the projectile script and try to get the stats component from the character.

Short version: make sure you run "GetComponent" on the correct gameObject (character in your case).

avatar image SepM · Jul 13, 2014 at 07:52 PM 0
Share

Are you sure that works? I've made projects before when I could use GetComponent between two different gameObjects at different positions. I'm using just plain GetComponent, not transform.gameObject.GetComponent.

1 Reply

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

Answer by Tehnique · Jul 13, 2014 at 08:11 PM

As stated in the comments, your problem is that you are using "GetComponent" which only looks on the current object for the component. You should use a reference to the object you want to find the component on and call "referencedObject.GetComponent".

I'm not sure how you got it to find components on other objects without referencing that object and using just "GetComponent", but it shouldn't. For more information look here.

Also, this read might help. Best thing to do is only call "GetComponent" in start and keep a private reference to the found component, so you don't have to call "GetComponent" multiple times in Update.

Comment
Add comment · 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

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

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Passing a Script Name to a Function 2 Answers

How to set a variable equal to another variable in another gameobject? 2 Answers

Why can't I change another script's variable with this script? Thanks, 1 Answer

AddComponent with parameter variable 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