- Home /
transform.Translate not working in if-statement?
Hey lovely experts,
I'm currently working on a basic 2D game. I want that when the User clicks a Button, a knife should shoot upwards.
However, my Code doesn't work. I tried a lot, but I don't see any mistake in the code. So it has to be something with the logic. Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawnhandling : MonoBehaviour {
public GameObject knife;
public GameObject knifePosition;
public float speed;
public bool shoot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("s")){ //just for prototyping
spawnKnife();
}
if(shoot == true){
knife.transform.Translate(0, speed, 0);
}
}
public void startShooting(){ // Function which will be called by Button
shoot = true;
}
public void spawnKnife(){
Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation );
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawnhandling : $$anonymous$$onoBehaviour {
public GameObject knife;
public GameObject knifePosition;
public float speed;
public bool shoot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$eyDown("s")){ //just for prototyping
spawn$$anonymous$$nife();
}
if(shoot == true){
knife.transform.position += new Vector3(0.0F, speed, 0.0F);
}
}
public void startShooting(){ // Function which will be called by Button
shoot = true;
print("shoot");
}
public void spawn$$anonymous$$nife(){
Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation );
print("spawn");
}
}
Try with this code. I added some print() methods to see if you actually call all the methods correctly and I also changed transform.Translate with transform.position +=...
print methods are working, but the knife is still not moving.
Have you tried to put simple Debug.Log to check if the startShooting function is called and the value of shooting in the Update funvtion?
Yep, the Log says that the functions are called, but somehow the knife still doesn't move.
is speed something else than 0 in the inspector?
Answer by LCStark · Sep 29, 2018 at 07:43 PM
knife is the prefab you're instantiating. In your Update method you are modifying the transform of the prefab instead of the spawned instance. Add a GameObject field to store reference to your instance:
GameObject knifeInstance;
public void spawnKnife() {
knifeInstance = Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation);
}
Then use that field to change the position:
void Update() {
...
if (shoot == true) {
knifeInstance.transform.Translate(0.0f, speed, 0.0f);
}
}
Thank you very much! That solved my problem. I would be really thankful If you could explain why exactly I had to do an instance first?
When an object is saved as a prefab, that prefab doesn't exist in the scene - it is treated the same way all other assets (like sprites, materials, models, scripts, etc.) are. You have to create an instance of this "template" to have a copy of the prefab in the scene. Your original code was doing the same thing - you used the Instantiate method in your spawn$$anonymous$$nife function. The only thing that was missing was a reference to that instance you were creating. Ins$$anonymous$$d of manipulating the created object, you were changing the transform of the prefab itself.
Your answer
Follow this Question
Related Questions
Sprite changing position when button pressed 1 Answer
Vector2.moveTowards but only on one axis 1 Answer
Difference between moving forward with transform.Translate vs transform.localPosition 2 Answers
Change size of an object up to a limit 2 Answers
Make GameObject only move when conditions are fulfilled 1 Answer