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 William_Weaver · May 01, 2014 at 05:33 AM · javascriptinstantiateprefabinventoryfind

Trouble working with a Custom class, JavaScript.

Another very important question. The scenario is this:

Player interacts with object to 'collect' it -> Object is an instance of a Prefab -> Object sends info about itself to player Inventory script -> Object deletes itself as if 'collected' -> Player instantiates said object later.

Edit(5/1/14): Getting the prefab via code is no longer a problem. (Originally I couldn't figure out how to have the item send it's own prefab to the inventory as a variable.) I've set up a custom Class that will hold each item's info, and I can simply drag the item's prefab to the correct spot on that item, then pass that variable of the new class to the inventory.

The issue I've run into now is trying to let the script set that class's variable. Unity's throwing me a lovely "Type 'ItemInfo' does not support slicing." As I understand it, this has to do with #pragma strict, and while removing that will help, it can cause issues down the road. Here's what I'm trying to do:

New Class:

 public class ItemInfo{
 
     var Name : String;
     var Tag : String;
     var PFB : GameObject;
     var Amount : int;
     var MaxStack : int;
     var Weight : float;
     var Durability : float;
     var Level : int;
 }

Item Script:

 #pragma strict
 public var info : ItemInfo;
 
 function Start () : void
 {
     info[0] = "Test Sword";//Name
     info[1] = this.gameObject.tag;//Tag
     //info[2] is prefab, will assign through inspector
     info[3] = 1;//Amount
     info[4] = 1;//MaxStack
     info[5] = 5.5;//Weight
     info[6] = 100;//Durability
     info[7] = 1;//Level
 }

Should I be assigning these values some other way? Like info.Name = "Test Sword"; ?

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

2 Replies

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

Answer by William_Weaver · May 01, 2014 at 06:15 AM

Yep. info.Name = "NameHere";

Guess I had to type it out to think it through. ^_^;

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

Answer by robertbu · May 01, 2014 at 05:37 AM

If you don't want to use drag and drop, you have two other choices:

  1. You can place the object in the Scene. It may be hidden by turning the renderer. And you may have to disable other components like the colliders. You can then use GameObject.Find() or GameObject.FindWithTag() to get access. You then use Instantiate() to make copies, and activate the renderer and any other components. Note you cannot deactive the object, because GameObject.Find() does not find deactivated objects.

  2. You can place the prefab in the Assets/Resources folder and loading it this way:


    GameObject shipPrefab = Instantiate(Resources.Load("Spaceship")) as GameObject;

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

20 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

Related Questions

How to instantiate a Prefab ? 2 Answers

Instantiate prefab within parameters?(Javascript) 1 Answer

Instantiate prefabs just before it comes into view 2 Answers

How to instantiate multiple prefabs in multiple positions? 0 Answers

Instantiate a Prefab thats been passed through 2 scripts? 2 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