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 eeveelution8 · May 11, 2014 at 07:34 PM · javascriptgetcomponentstringfind

Access a GameObject through it's tag, using a string.

In a weapon pickup script i have, The script i have should find a gun, based upon it's tag. In order to reuse this script instead of having 9 different ones, I added a string variable called gunname.

 var gun : GameObject;
 var myself : GameObject;
 var player : GameObject;
 var gunname : String;
 var ammo : gunswitcher;
 
 var isweapon1 : boolean = false;
 var isweapon2 : boolean = false;
 var isweapon3 : boolean = false;
 var isweapon4 : boolean = false;
 var isweapon5 : boolean = false;
 var isweapon6 : boolean = false;
 var isweapon7 : boolean = false;
 var isweapon8 : boolean = false;
 var isweapon9 : boolean = false;
 
 var deadtime : float = 30;
 
 var x : float = 0;
 var y : float = 0.25;
 var z : float = 0;
 
 function OnTriggerEnter() {
 
 gun.SetActive(true);
 activate();
 turnoff();
 
 }
 
 function turnoff() {
 
 collider.enabled = false;
 myself.SetActiveRecursively(false);
 
 yield WaitForSeconds(deadtime);
 
 collider.enabled = true;
 myself.SetActiveRecursively(true);
 }
 
 function Start() {
 
 InvokeRepeating("reset",0,0.25);
 
 }
 
 
 function reset()
 {
     player = GameObject.FindWithTag("Player");
     ammo = player.GetComponent(gunswitcher);
     gun = GameObject.FindWithTag("gunname");
         
 }
 
 function Update() {
 
     transform.Rotate(x, y, z);
 
 }
 
 
 function activate() {
 if(isweapon1 == true){
 ammo.gunactive1 = true;
 }
 else if(isweapon2 == true){
 ammo.gunactive2 = true;
 }
 else if(isweapon3 == true){
 ammo.gunactive3 = true;
 }
 else if(isweapon4 == true){
 ammo.gunactive4 = true;
 }
 else if(isweapon5 == true){
 ammo.gunactive5 = true;
 }
 else if(isweapon6 == true){
 ammo.gunactive6 = true;
 }
 else if(isweapon7 == true){
 ammo.gunactive7 = true;
 }
 else if(isweapon8 == true){
 ammo.gunactive8 = true;
 }
 else if(isweapon9 == true){
 ammo.gunactive9 = true;
 }
 
 
 }

Everything else in the script except line 53 works. I've tried every permutation of the script I know, but nothing has worked. I need help with this.

Comment
Add comment · Show 4
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 Li0nSword · May 11, 2014 at 09:39 PM 0
Share

If you are trying to use string shouldn't it be like this:

gun = GameObject.FindWithTag(gunname);

avatar image eeveelution8 · May 11, 2014 at 10:54 PM 0
Share

Already tried that, didnt work.

avatar image MrFijiWiji · May 11, 2014 at 11:41 PM 0
Share

The way you have it written now, you're looking for a gun with a tag of "gunname" which i'm sure you don't want.

as Li0nSword said, you should be accessing it by (gunname) and not ("gunname"). Are you setting gunname in the inspector or via code?

avatar image MikeNewall · May 12, 2014 at 12:09 AM 0
Share

Do you have each tag setup in the tag manager? Ins$$anonymous$$d of searching by tag you could use the name of the object so you don't have to setup tags.

 GameObject.Find(gunname);

Just make sure the object is named the same as the search string

1 Reply

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

Answer by oliver-jones · May 12, 2014 at 02:10 PM

Replace your:

 gun = GameObject.FindWithTag("gunname");

To:

 gun = GameObject.FindWithTag(gunname);

As already mentioned before, you're actually searching for a GameObject with the tag 'gunname', whereas you want to use the variable called 'gunname'. Also, when you run it - check in the inspector if the gunname variable is populating.

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 eeveelution8 · May 15, 2014 at 02:13 AM 0
Share

Still Doesn't work

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

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

Related Questions

Why do I have to call ToString() when fetching a String from another script? 2 Answers

Finding A Variable With A String Variable 1 Answer

GetComponent: Easy help 1 Answer

Point system help! 2 Answers

Passing a string from Javascript to C# 1 Answer


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