- Home /
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
}
}
}
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.
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.
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.
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 ;)
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?)
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];
}
}
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
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Using an array instead 2 Answers
Arraylist to GameObject[] array issue. 1 Answer