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 Winston · Feb 18, 2011 at 07:57 PM · errorinstantiateclassargumentexception

Instantiate in a class

Hi, I am farely new to Unity but am having problems with some scripting in Javascript.

I am trying to instantiate and object of a prefab in a class. My current code is as follows:

class unitLine { public var soldierPre : Transform;

 function unitLine()
 {
     GameObject.Instantiate(soldierPre);
 }

};

var Line1 : unitLine;

function Awake() { Line1 = new unitLine(); }

This gives the error "ArgumentException: The thing you are trying to instantiate is null". I have the prefab object applied in the hierachy and not the script itself.

I have searched for more than 2 hours trying to find a solution, but just keep error hopping.

I am also having some problems tryingto access a global variable in my class. Is it possible?

Many thanks to those who can help.

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

1 Reply

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

Answer by Jessy · Feb 18, 2011 at 08:15 PM

soldierPre is serialized within Line1. When you create a new unitLine, the soliderPre of that unitLine is null. I don't know what you're actually doing, but this could work:

(Your capitalization does not adhere to standards so I modified that.)

class UnitLine { var soldierPre : Transform;

 function UnitLine (soldierPre : Transform) {
     this.soldierPre = soldierPre;
     UnityEngine.Object.Instantiate(soldierPre);
 }

}

var line1 : UnitLine;

function Awake() { line1 = UnitLine(line1.soldierPre); }

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 Winston · Feb 18, 2011 at 08:22 PM 0
Share

I tried defining soldierPre as a global variable above the class but I could not find a way to access it within the class. Any ideas?

avatar image Jessy · Feb 18, 2011 at 08:44 PM 0
Share

I have no idea what you're doing, but the code above works, at least. Describe what you actually want to do, ins$$anonymous$$d, because this code looks useless.

avatar image Winston · Feb 18, 2011 at 09:41 PM 0
Share

Thanks that code worked fine. I completely forgot about passing the constructor an argument of the transform object. Stupid mistake.

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

No one has followed this question yet.

Related Questions

c# Unexpected symbol '=' in struct, class..... 1 Answer

Using JS class in C# 1 Answer

How do I fix this error code: BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(System.Collections.Generic.List., UnityEngine.Vector3, UnityEngine.Quaternion)' was found. 2 Answers

Scripting errors only when building for iPhone 2 Answers

Enemy not spawning correctly 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