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 davidflynn2 · Aug 14, 2013 at 09:17 PM · c#arrayconvert

Convert to array.

I am still fightiging to full get the hang of arrays and one help me convert this over to one. What I different tags on my elements in the scene and I want to do a Enemy array and alliance array and type the name of each tag into each category of array and then set the scope color by what group they are in. Here is the code as it is right now.

 using UnityEngine;
 using System.Collections;
 
 public class Targeting : MonoBehaviour {
 public GameObject scope; // The main score that is set infront of the ship to allow aiming of ship
 private int Range= 70; // The range of the array
     
     void  Update ()
     {
         RayShoot();//Stast the rayShoot array
     }
     void  RayShoot ()
     {
         RaycastHit hit;
         
         Vector3 facingDirection= transform.TransformDirection(Vector3.forward);
         Debug.DrawRay(transform.position, facingDirection * Range, Color.blue);
         scope.renderer.material.color = Color.blue;
         
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Enemy") //Checks the tag
         {
             scope.renderer.material.color = Color.red; //Sets the scope color red
         
            }
         
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Alliance") //Checks the tag
            {
             scope.renderer.material.color = Color.green; //Sets the scope color green
 
         }
         
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Meteor") //Checks the tag
            {
             scope.renderer.material.color = Color.red; //Sets the scope color red
         }
 
         
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceMothership") //Checks the tag
            {
             scope.renderer.material.color = Color.green; //Sets the scope color green
         }
         
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyMothership") //Checks the tag
            {
             scope.renderer.material.color = Color.red;  //Sets the scope color red
         }
         
        if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemySatellite") //Checks the tag
            {
             scope.renderer.material.color = Color.red;  //Sets the scope color red
 
         }
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceSatellite") //Checks the tag
            {
             scope.renderer.material.color = Color.green;  //Sets the scope color green
         }
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AlliancePlanet") //Checks the tag
            {
             scope.renderer.material.color = Color.green;//Sets the scope color green
         }
         if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyPlanet") //Checks the tag
            {
             scope.renderer.material.color = Color.red; //Sets the scope color red
         }
     }
 }
 
Comment
Add comment · Show 7
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 flaviusxvii · Aug 14, 2013 at 09:20 PM 1
Share

This question is really similar to your other question about damage. Are you looking for an excuse to use an array, because honestly I'd use almost the same approach to this as I suggested for the other question.

avatar image Joyrider · Aug 14, 2013 at 09:23 PM 1
Share

Yep indeed... I was just looking into that...

Read the answers to your previous question again. $$anonymous$$ake an attempt at co$$anonymous$$g up with a solution.

And we'll start from there ;) It's the best way to learn.

avatar image davidflynn2 · Aug 14, 2013 at 09:27 PM 0
Share

Well I have tried to convert it to make this code more smaller and optimized but I cant seem to figure out what to put at the end off raycast part of this line here is what I changed.

 public string[] enemy = new string [5]{"Enemy", "$$anonymous$$eteor", "Enemy$$anonymous$$othership", "EnemySatellite", "EnemyPlanet"};
     public string [] alliance = new string[4] { "Alliance", "Alliance$$anonymous$$othership" ,"AllianceSatellite", "AlliancePlanet"};
 
 
 if Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == enemy[])
         {
             scope.renderer.material.color = Color.red; //Sets the scope color red
         }


This just floods it with errors.

avatar image Joyrider · Aug 14, 2013 at 09:34 PM 1
Share

Okay, well at least we've got something to work with.

Now, two things:

1/ you did create arrays, but you never iterate through their contents. (with a for-loop for example)

2/ by having your raycast and your tag check in the same if-statement, you'll be doing a lot of raycasts (1 per tag-check), while it is always the same ray you're casting. So, first do your raycast and if you hit something, use the result to check what situation you're in ;)

avatar image davidflynn2 · Aug 14, 2013 at 10:16 PM 0
Share

Ok so I have redone it some and this is what I have now:

 using UnityEngine;
 using System.Collections;
 
 public class Targeting : $$anonymous$$onoBehaviour {
 public GameObject scope; // The main score that is set infront of the ship to allow ai$$anonymous$$g of ship
 private int Range= 70; // The range of the array
     
     
     public string[] enemy = new string [5]{"Enemy", "$$anonymous$$eteor", "Enemy$$anonymous$$othership", "EnemySatellite", "EnemyPlanet"};
     public string [] alliance = new string[4] { "Alliance", "Alliance$$anonymous$$othership" ,"AllianceSatellite", "AlliancePlanet"};
     
     void  Update ()
     {
         RayShoot();//Stast the rayShoot array
     }
     void  RayShoot ()
     {
         RaycastHit hit;
         
         Vector3 facingDirection= transform.TransformDirection(Vector3.forward);
         Debug.DrawRay(transform.position, facingDirection * Range, Color.blue);
         scope.renderer.material.color = Color.blue;
         
         if(Physics.Raycast(transform.position, facingDirection,out hit, Range))
         {
             foreach(string go in enemy)
             {
 
                 if(hit.transform.gameObject.tag == enemy.tag)
                 {
                     scope.renderer.material.color = Color.red;
                 }
             }
             foreach(string go in alliance)
             {
                     
                 if(hit.transform.gameObject.tag == alliance.tag)
                 {
                     scope.renderer.material.color = Color.green;
                 }    
             }
                 
         }
         
     }
 }


But I get the following error. Assets/SpaceNubula/Scripts/Targeting.cs(38,74): error CS1061: Type string[]' does not contain a definition for tag' and no extension method tag' of type string[]' could be found (are you missing a using directive or an assembly reference?)

Show more comments

1 Reply

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

Answer by flaviusxvii · Aug 14, 2013 at 09:28 PM

If you are REALLY gung-ho about using an "array" for this, I'd actually suggest using a Dictionary.

 using System.Collections.Generic; // <-- need this up top!!
 
     // Make a dictionary, it'll map strings to colors..
     public Dictionary<string, Color> colorKey = new Dictionary<string, Color>();
 
     // somewhere you need to populate the registry
     colorKey["EnemyPlanet"] = Color.red;
     colorKey["Cheese"] = Color.yellow;
     colorKey["Chewbacca"] = Color.brown;
 
     // In your RayShoot function use this..
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range)) {
         // see if the tag is in our registry
         if(colorKey.ContainsKey(hit.transform.gameObject.tag)) {
             // lookup the color stored for this tag
             scope.renderer.material.color = colorKey[hit.transform.gameObject.tag];
         }
     }
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 davidflynn2 · Aug 14, 2013 at 10:54 PM 0
Share

Not actually what I ended up using but this did work when I set it all up and got it in my scene. I ended up extending the DamageInflictor script you helped me with in the other post. Thanks so much for your help and have a great day.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Using an array instead 2 Answers

Arraylist to GameObject[] array issue. 1 Answer

Lightning Script Js To C# Conversion errors 2 Answers

How to select adjacent cubes along edges? 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