- Home /
Array Problem: can't add Component
Hello everyone, I can't figure out why I am having this problem! I have tried this with a game object and the code is like this:
var prefab:GameObject; function Start () { prefab = Instantiate (myHouse, gameObject.transform.position , Quaternion.identity); prefab.transform.parent = transform; var script : ChangeCursor = prefab.AddComponent(ChangeCursor); script.OverCursor = OverCursor; script.DragCursor = DragCursor; }Everthing works fine here! But I am trying the same method with array and I don't see any change! Here's the code:
var next:GameObject = Instantiate(myArray[0],transform.position , Quaternion.identity); next.transform.parent = transform; var script : ChangeCursor = next.AddComponent(ChangeCursor); next.OverCursor = OverCursor; next.DragCursor = DragCursor;I see next created when I run the game, also its a child to transform. The only part not working is adding the component next is a gameobject just as the prefab in the first code! So what might be the problem? do i have to attach the component(script) to the whole array? or what else? plz some help
Btw. try to find better variable names. The variable you called "prefab" is used to hold an instance and not a prefab. Also "next" isn't very dexcriptive in this situation. Even "clone" or "obj" would fit better ;)
Same goes with "myArray". You can't deter$$anonymous$$e anything from that name except it's yours and it's an array. Use functional names. If it holds house prefab references, call it "housePrefabs" "houseArray" or "housePrefabArray".
It's called a program$$anonymous$$g "language". Like in every language you should find words that express what you want to say.
I could call my table "A" my door "B" and my chair "C". The sentence: "Someone stepped through my B sits on my C and eats from my A" is not wrong, but no one will understand you ;)
A lot people argue that they are the only ones that work with that code, but even then when you look at a piece of code that you've written a year ago, you will have problems to figure out what you've done there.
you are absolutly right, although i called some of these for testing my code, like .. let it work then I rearrange my variables
:D yep i know what you mean. Usually for quick testing i use simply the first letter of the real name. "O" or object, "C" for clone, "i" for instance or index. I just want to have that said ;)
Answer by aldonaletto · Jun 01, 2012 at 12:20 AM
What's wrong in the second script is the assignment of OverCursor and DragCursor: you're using next instead of script - they should be:
var next:GameObject = Instantiate(myArray[0], transform.position, Quaternion.identity); next.transform.parent = transform; var script : ChangeCursor = next.AddComponent(ChangeCursor); script.OverCursor = OverCursor; // <- these assignments were wrong! script.DragCursor = DragCursor;NOTE: I'm supposing that myArray is an GameObject array containing the prefabs - is it ok?
@AldoNaletto ,Thanks for the response. We'll I have to add the OverCursor and DragCursor to my script and not to my prefab, isn't that right? since the same code works working with the prefab! Do i have a copy and paste mistake or you edited that? it has to be script.OverCursor
and i'm sure myarray is a GameObject array and I've added my prefabs to it in the inspector
Well, I see that my problem is not array related!! The prefabs that I am adding to myArray in the inspector hold modes imported, I replaced them with prefabs that hold cubes, it simply worked. So this is an another problem!
Yes, arrays or single variables work the same - there's something wrong elsewhere.
The two last assignments seem too suspect to me: what are OverCursor and DragCursor? If they are scripts that you want to add to the object, you should use AddComponent ins$$anonymous$$d:
... next.AddComponent(OverCursor); next.AddComponent(DragCursor);