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 El Maxo · Feb 17, 2015 at 09:29 AM · c#instantiatespawning

Trouble with Instantiation of objects

Hi,

I am just trying to set up a prototype of spawning a player in a location of his choice. This is the first time I have tryed to Instantiate a object in c# and I am a bit lost after looking at the documentation. At the moment I am trying to make the "player" object spawn in a location, either "Room1Spawn" or "Room2Spawn" after the room has been pressed, my code is shown bellow.

But I am having some errors pop up that I cant quite figure them out:

Assets/Scripts/RoomSpawn.cs(17,57): error CS1061: Type UnityEngine.GameObject' does not contain a definition for position' and no extension method position' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

I assume this is me not declaring something right (have this issue with both position and rotation of both objects). I also have :

Assets/Scripts/RoomSpawn.cs(17,25): error CS1503: Argument #2' cannot convert object' expression to type `UnityEngine.Vector3'

This backs up my previous theory.

My Code:

 using UnityEngine;
 using System.Collections;
 
 public class RoomSpawn : MonoBehaviour {
 
     public GameObject Room1;
     public GameObject Room2;
     public GameObject Room1Spawn;
     public GameObject Room2Spawn;
     public GameObject Player;
 
 
 
 
     void OnMouseDown (Collider other){
         if (other.gameObject == Room1) {
             Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);
         }
         if (other.gameObject == Room2) {
             Instantiate (Player, Room2Spawn.position, Room2Spawn.rotation);
         }
     }
 
 }




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 El Maxo · Feb 17, 2015 at 11:12 AM

Ok found out what the issue was that I was storing it as a game object. I looked at some tutorials and they were making the targets as transforms .

 public Transform Room1Spawn;


 Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);

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 Mmmpies · Feb 17, 2015 at 11:21 AM 0
Share

Yep or reference the transform as I said in my last edit. Either's fine.

avatar image
0

Answer by Mmmpies · Feb 17, 2015 at 09:33 AM

Not got access to Unity but try casting that instantiate as a GameObject like this:

 (GameObject)Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);

Can't test but from memory that should be OK. Or at least give you a different error ;¬)

Comment
Add comment · Show 6 · 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 El Maxo · Feb 17, 2015 at 09:36 AM 0
Share

Thanks for help, ill test in a $$anonymous$$. Just a quick question thought, just so I can learn, what is the first (GameObject) for?

avatar image Mmmpies · Feb 17, 2015 at 09:42 AM 0
Share

Just says to the compiler that you want it to treat it as a GameObject, really just a way of forcing an acknowledgement that this is what you're trying to do even if the Instantiate contains a Vector3. C# being strict and saying I'm not sure you want to do that, so you cast it as a GameObject says "yes I do".

avatar image El Maxo · Feb 17, 2015 at 09:47 AM 0
Share

Thanks for explaining, althought I have a new error :

Assets/Scripts/RoomSpawn.cs(18,99): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

avatar image Mmmpies · Feb 17, 2015 at 09:52 AM 0
Share

Ah missed that, that's why doing things from memory is a bad idea, well it is for my memory!

 GameObject newPlayer = (GameObject)Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);

You have to instantiate it into a GameObject so just set a temporary GameObject like I have with newPlayer.

avatar image Mmmpies · Feb 17, 2015 at 10:26 AM 0
Share

Ah you're checking the collider but that's a child of the GameObject you drag onto the public slot, again from memory (so expect errors)

 if (other.transform.parent.gameObject == Room1)

or just tag it and check for the tag.

EDIT

That's O$$anonymous$$, try putting a Debug.Log in to make sure it's picking up your click and post your code for the On$$anonymous$$ouseDown so we're working with the new code.

Oh and try just instantiating at one of (or both of) the locations without a click, at least that'll test the original question is not the issue.

EDIT 2

Damn I really need Unity to test these basic errors! We've missed the transform from the Spawn location:

 GameObject newPlayer = (GameObject)Instantiate (Player, Room1Spawn.transform.position, Room1Spawn.transform.rotation);

These should autocomplete as you type depending on what editor you use.

Show more comments

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

21 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

Related Questions

Instantiating at different rates 1 Answer

Distribute terrain in zones 3 Answers

Player 2 not spawning 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

Instantiate a certain amount of prefabs 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