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 PaxForce · Feb 26, 2014 at 12:31 PM · reference

creating reference via script

What is the code/script equivalent of dragging an item to a slot in screen view? For example, we can manually set to use gravity for a rigidbody, but we also can set this in code:

 rigidbody.useGravity = true;

We can create a GameObject in our script and then drag and drop myItem to the new slot manually in the scene view. I would like to ask you how to code this “drag&drop”.

===edited on 13. march 2014 ===== here goes: I have about 20 spell prefabs in my "spells" folder.

This is my variable to which I can assign one of my 20 prefabs from assets, in order to play a specific animation.

 public GameObject _spell;

I would like to assign via script a certain animation. For example, if my player picks up a "spell_Fire" PickUp, I would like Unity to dynamically assign the fire animation prefab to my _spell variable.

Comment
Add comment · Show 1
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 fafase · Mar 13, 2014 at 07:18 PM 0
Share

I would assign the spell object to the pick up object and then while hitting the pick up you pass that reference to the player.

2 Replies

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

Answer by Spoink · Feb 26, 2014 at 01:05 PM

I'm not really sure what you mean with "dragging an item to a slot" But there are two "slots" I can think of.

Either you have an unassigned object

 public GameObject MyObject

this will show up as MyObject [None (GameObject)] in the inspector.

To assign an object to this variable, you just have to do normal variable assigning

 void Awake()
 { this.MyObject = GameObject.Find("ObjectToAssign"); }

The second "slot" I can think of is if you want to assign a component to your object. Adding a rigidbody, box collider or a script.

To do this you use the AddComponent-function.

 gameObject.AddComponent<BoxCollider>();
Comment
Add comment · Show 3 · 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 PaxForce · Mar 13, 2014 at 06:14 PM 0
Share

doesn't work

avatar image fafase · Mar 13, 2014 at 06:19 PM 0
Share

"doesn't work" doesn't tell much what does not work. You want to know the code but he already told you what you are after. The only part missing is if you are after dragging a script, it would then be:

 Type t = GetComponent<Type>();
avatar image PaxForce · Mar 13, 2014 at 06:41 PM 0
Share

I'll explain it in the question by editing it.

avatar image
0

Answer by pcaston2 · Oct 11, 2014 at 03:45 AM

To elaborate on the solution I used, first I created an abstract class that inherits MonoBehaviour for all my types. (Hull, Engine, etc.). Within the abstract class I specified the sub-components (both a GameObject for the mesh and a Transform for the mount point) and a method to add a new one. All my meshes are in Resources folders and I simply initialize them using the Resources.Load() method, and pass the name. From each GameObject I can get the script by saying Hull.engine.GetComponent(typeof(Engine)) or build a property that accesses it for compact code.

I was able to assemble many components using this, they are modular, but each one needs an script that inherits from the abstract class in order to specify the mount points. I suppose I could tag the mount points and find them dynamically but this works just as well.

Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script only makes character run forward 1 Answer

mouse dissapear 1 Answer

Csharp: how to make script wait for x seconds 3 Answers

How do I call a function in another gameObject's script? 5 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