FindGameObjectsWithTag returns an array of objects rather than one. So you’d have to loop through the list and change them all, or decide which one(s) to change somehow. If you just want the first object that gets returned as a normal variable (not an array), use FindWithTag instead.
Thanks for the explaination. Actually I want all of the remaining objects with that tag to be effected. I was calling them individually but they can get destroyed during the game and I end up with a null reference problem. I had hoped tag would work on any of them that are left…
Aha, in that case you do want to get an array and then loop through it, so you can work with each object in the array. So do something like:
var changeV : GameObject[] = GameObject.FindGameObjectsWithTag ("VObj");
for (v in changeV) {
v.renderer.material.color.a = 1;
}
You can leave out the type definition for changeV, but that makes it clear what kind of variable it is (i.e., an array of objects instead of just one).
Awesome! I have seen that kind of script before but never really got it. Is there a term for that kind of action and are there any good references online to study up on them? The script you provided works perfectly, but for the improvement of my own skills I would like to understand things better. In fact, Eric, as you always have an answer for any programming question I come up with, do you have any recommendations on books or references to help me my programming skills a step or two closer to yours?
Thanks at any rate for helping me with this!
I believe the best term is “iteration”. Another way of doing the same loop as above would be:
for (i = 0; i < changeV.Length; i++) {
changeV[i].renderer.material.color.a = 1;
}
It’s sometimes more convenient to iterate through the array by using the “for x in y” construction, when you don’t really care about the index numbers and just want to change all of the elements in the array. It’s basically saying “for every item in the array, do this.”
Unfortunately I’m not really familiar with what might be the better resources for that sort of thing, either online or books. I just tend to pick stuff up from here and there, I guess. Maybe someone else can suggest something?
I would recommend the book, Game Programming All In One, as a good step in getting familiar with basic programming concepts such as this. It also has the advantage of being focused mainly on the application towards game development and it ramps up from basic to advanced quite well. I have been a programmer for 17 years and I still use this book fairly often to brush up on my C/C++ and game related coding. (C is fairly similar to Javascript so it shouldn’t be too difficult to translate the examples; I’m sure there are some resources out there to help you with that as well).
I found it on Amazon:
This link has some other related books from this series that might also interest you. Search around there and see what looks useful to you. I have ordered several computer books from amazon and their “best price” alternatives and have been pleasently surprised at the cost and selection/availability versus computer book stores. Delivery didn’t take that long either; I am one of those who wants it right now but some books I wasn’t able to find in stores and certainly not at the great prices I found.
Nobody is probably watching this thread anymore but I just had to thank Eric5h5 for his replies to this post. I found your snippet of code re FindGameObjectsWithTag well past midnight after a frustrating day of coding! Your posts have helped me many times before but this morning was the best! At work I develop in EON Studio and their forums are practically non-existent. All I can say is thank goodness for the Unity Forums and guys like you!