Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by severedskullz · Aug 16, 2011 at 04:49 PM · errorinstantiatevarcs1061typecast

Type casting issue with var keyword

 using UnityEngine;
 public class PopulateAsteroids : MonoBehaviour {
 public int NumAsteroids;
 public GameObject gAsteroids;
 // Use this for initialization
 void Start ()
 {
     var Asteroid = Instantiate(gAsteroids);
     Asteroid.transform.position = Vector3(1,1,1);
 }
 }

Why is this not working? It keeps telling me: "Assets/Scripts/PopulateAsteroids.cs(11,26): error CS1061: Type UnityEngine.Object' does not contain a definition for transform' and no extension method transform' of type UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)"

Why is it not instantiating to a GameObject even though I explicitly tell it to? New to unity, sorry.

I am aware that I can specify the position in the instantiate but that is not the point. I want to do so much more within the code.

Edit: How the HELL do you use the code snippets? they are not working for me.

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

Answer by Joshua · Aug 16, 2011 at 05:04 PM

Hey there, welcome to UA.

I fixed your code snipped, next time select all of it and hit the [1010101] button or put four spaces in front of every line.

You seem to be using C#'s var keyword but not understanding what it does. It'll cast a variable to what is being written to it, in this case you use Instantiate() which returns an Object. You then try to access this objects .transform, which doesn't exist. What you want to do is tell the variable you're writing Insantiate to that it's a GameObject.

 var Asteroid = Instantiate(gAsteroids) as GameObject;

or

 var Asteroid = (GameObject)Instantiate(gAsteroids);

it's usually best do use the as keyword to downcast as it will return null if it can't preform the cast. Do note that using the var keyword is considered by many as bad style in C#, since you can't obviously see what type the variable is. So I would personally advice to do it without it:

 GameObject Asteroid = Instantiate(gAsteroids) as GameObject;
 


Comment
Add comment · Show 2 · 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 severedskullz · Aug 16, 2011 at 05:29 PM -1
Share

Thank you so much! I was at the point of punching my screen in. I've been following tutorials on different topics and have just been mashing them together. Learning as I go.

avatar image Joshua · Aug 16, 2011 at 05:31 PM 0
Share

No problem, happy to have helped and feel free to keep asking questions here. For future reference though, please accept answers if they've solved your issue (this closes the question) and don't post comments as new answers (this clutters the question for future reads who have the same problem).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Enemy not spawning correctly 1 Answer

Is there anything wrong with this script? It used to work but now it doesn't. 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Can´t instantiate objects in list 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