- Home /
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 methodposition' 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);
}
}
}
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);
Yep or reference the transform as I said in my last edit. Either's fine.
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 ;¬)
Thanks for help, ill test in a $$anonymous$$. Just a quick question thought, just so I can learn, what is the first (GameObject) for?
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".
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
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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