Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed May 01, 2013 at 05:42 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Luckd93 · May 01, 2013 at 05:08 PM · variablegetcomponentother script

Get variable from other GameObject's script

I tried to get the variable nbullet from the script Control of the GameObject Player, I wrote this script:

 using UnityEngine;
 using System.Collections;
 
 public class Info : MonoBehaviour {
 
 public int nbullet;
 public GameObject player;
 void Start () {
 }
 void Update () {
     player = GameObject.Find("Player");
     nbullet = GetComponent("Control").nbullet;
     }
 }

but it tells me this error:

Assets/Info.cs(29,52): error CS1061: Type UnityEngine.Component' does not contain a definition for nbullet' and no extension method nbullet' of typeUnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

why? how can I fix this?

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

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by fafase · May 01, 2013 at 05:42 PM

You should learn how to use this site. This has many answers all over the place.

Also look there: http://unitygems.com/script-interaction1/ http://answers.unity3d.com/questions/246211/having-scripts-interact.html

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

Answer by ExTheSea · May 01, 2013 at 05:11 PM

If the nbullet variable is public you just have to cast the Component to the script like this:

 var controlscript : Control = player.GetComponent("Control");
 nbullet = controlscript.nbullet; //You should also consider renaming your nbullet variable
Comment
Add comment · Show 5 · 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 Luckd93 · May 01, 2013 at 05:26 PM 0
Share

what kind of variable is Control? I can't use it.

avatar image ExTheSea · May 01, 2013 at 05:33 PM 0
Share

Nvm you're using C#. Control is the script so the variable type is called Control. In your case it would be:

 Control controlscript = player.GetComponent<Control>();

If you want to read about how to access variable from other scripts follow this link: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image Luckd93 · May 01, 2013 at 05:45 PM 0
Share

thx,now it run but it stop telling:

NullReferenceException UnityEngine.GameObject.GetComponent[Control] () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:180) Setting.Update () (at Assets/Setting.cs:40)

what's a NullReferenceException ?

avatar image ExTheSea · May 01, 2013 at 05:59 PM 0
Share

A NullReferenceException occurs when you reference something in your script which doesn't exist. In this case maybe there is no Control script attached when you're using getComponent.

avatar image Luckd93 · May 01, 2013 at 06:22 PM 0
Share

Thx I found the error, I was searching the GameObject Player but instantiating the Player it was named Player(Clone). Thank you really much

avatar image
0

Answer by ahsen35813 · Mar 30, 2020 at 02:53 PM

I've spotted the issue. Everything is correct except for line 12. You wrote nbullet = GetComponent("Control").nbullet. This is almost correct. You forgot to GetComponent from the player, and I think the GetComponent could potentially have incorrect syntax. So it should be more like this: nbullet = player.GetComponent<Control>().nbullet;

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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Accessing a variable within a function.... GetComponent() 2 Answers

GetComponent from string name? 3 Answers

Only change a variable on the instaniated object not the prefab. 0 Answers

getComponent not working with Triggers? 1 Answer

Variable from a external script not being effected 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