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 moghes · May 31, 2012 at 11:59 PM · arrayaddcomponent

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
Comment
Add comment · Show 3
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 Bunny83 · Jun 01, 2012 at 03:06 AM 1
Share

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.

avatar image moghes · Jun 01, 2012 at 03:33 AM 0
Share

you are absolutly right, although i called some of these for testing my code, like .. let it work then I rearrange my variables

avatar image Bunny83 · Jun 01, 2012 at 03:40 AM 0
Share

: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 ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?
Comment
Add comment · Show 4 · 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 moghes · Jun 01, 2012 at 12:38 AM 0
Share

@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

avatar image moghes · Jun 01, 2012 at 12:40 AM 0
Share

and i'm sure myarray is a GameObject array and I've added my prefabs to it in the inspector

avatar image moghes · Jun 01, 2012 at 01:14 AM 0
Share

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!

avatar image aldonaletto · Jun 01, 2012 at 11:04 AM 0
Share

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);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# how to add custom class object to array 0 Answers

AddComponent To GameObjects On Array 1 Answer

how can i check if there is a blanc spot in my list 3 Answers

Custom Inspector GUI, Instantiating some classes 2 Answers

Instantiate specific object from "FindGameObjectsWithTag" Array 0 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