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 s2jesse · Feb 28, 2011 at 10:06 PM · javascriptprefabsvariablesclassclasses

Linking prefabs to classes that are not attatched to gameobjects

I'm learning to use classes using java. Im trying to create a system to manage npc spawning etc. Basically im creating a class and instantiating it in my main game script which is attached to a gameobject.

i want to assign a bunch of prefabs to various transform variables in my class. My class is not attached to an object but the interface allows you to hook up prefabs to the variables in the class itself.

Now i have this code in the class.

class MyClass { public var thisPrefab : Transform;

 function MyClass() 

{

 }   

 function getPrefab()
 {       
     return thisPrefab;
 }   

}

now in my main game script i and trying to basicaly do this

var npc = new MyClass();
function foo()
{
    enemyPrefab = npc.getPrefab;
    Instantiate(enemyPrefab, enemySpawn.transform.position, enemySpawn.transform.rotation);
}

Im getting the error "the prefab you want to instantiate is null.

I know your supposed to connect the prefab to the script once its attached to a gameobject but with a class i dont see how to do this. Not sure where to go from here. Is there a way to dynamically choose prefabs without manually linking them in the editor?

Thank you. Jesse

Comment
Add comment · Show 2
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 Jessy · Feb 28, 2011 at 10:19 PM 0
Share

http://www.ericgiguere.com/articles/javascript-is-not-java.html You don't need to use position : Vector3, rotation : Quaternion. Just use the one-parameter version of Instantiate once you get it working.

avatar image s2jesse · Feb 28, 2011 at 11:51 PM 0
Share

good call thank you!

1 Reply

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

Answer by Alec-Slayden · Feb 28, 2011 at 11:35 PM

if you create a folder called "Resources" in your assets, you can use Resources.Load

Comment
Add comment · Show 1 · 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 alibaadshah · Oct 10, 2014 at 10:25 PM 0
Share

please i need video this answer

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

1 Person is following this question.

avatar image

Related Questions

Custom JS class error - function is not a member of the class 2 Answers

How to properly change variables of a sub class 0 Answers

hot to make classes and why ? 1 Answer

Calling variables in classes from other scripts 2 Answers

Multiple classes inside each other 1 Answer


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