I have an object that spwans other objects. I am referencing with the variable myprefab. I try to look for the sprite I want to reference in my asset folder, there is nothing in my asset folder. I try to drag the sprite directly onto the variable and it didn’t work. Why is that?
public Rigidbody2D myprefab;
public Transform myObj;
int y =0;
void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.Mouse0)){
y+=1;
Rigidbody2D Square1;
Square1 = Instantiate(myprefab, new Vector3(myObj.position.x,y,0), Quaternion.identity);
}
}
This is because Sprite is entered into a variable of RigidBody2D format.
It would be a good idea to change myprefab to sprite format or add a new variable.
Your field is of type Rigidbody2D. A sprite isn’t a rigidbody. You need to ensure that you’re dragging either a rigidbody2d component, or a game object that has a ridigbody2d component on it. Maybe you need to set up a prefab first?
I am able to assign the sprite in the field, but nothing gets instantiated when I am clicking.
I tried adding as Rigidbody2D to the end of the instiate statement, but I got typemismatch.
public Sprite myprefab;
void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.Mouse0)){
y+=1;
Sprite Square1;
Square1 = Instantiate(myprefab, new Vector3(myObj.position.x,y,0), Quaternion.identity);
}
}