- Home /
UNITY 5 - SPACE SHOOTER - INSTANTIATE NOT SPAWNING
Hi Everyone, new here.
Bolts Instantiates in space shooter won't spawn, even though my code is EXACTLY the same as in the video script and change log doesn't mention any change between unity 5 and previous version for this part.
It won't spawn, let alone be spawned through get.button..
just started coding, already getting frustrated hehe !
Hope someone will help,
Thank you !
Ok, after trying many alternatives, i found the problem and the solution so i'll just post it here for future users who might encounter this problem :
Don't write the code inside " void Update() " as shown in the video, for some reason it does compile but it's like when you run the game, it goes past this command.
Write it with the rest under "void Fixedupdate()" !
$$anonymous$$oderators : you should make this part of the changelog ;)
$$anonymous$$indly,
BigWood !
That really isn't proper fix as in FixedUpdate there is a chance that game doesn't register your button press at all. I can't imagine any reason why it wouldn't work in Update. By any chance could you paste code where it doesn't work inside Update().
Paste the code.... Putting it in FixedUpdate() isn't a solution.
Please post your code. Putting the code in FixedUpdate() isn't a proper fix.
Answer by AceWeston · Mar 07, 2017 at 04:26 PM
I have the same issue. Shots are spawned (I can see them in the hiearchy, and the position is changing) but they do not show in the game view. If i just drag and drop a shot into the game hierarchy it shows as intended though.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary {
public float xMin, xMax, zMin, zMax;
};
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Start(){
rb = GetComponent<Rigidbody> ();
}
void Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire){
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position , shotSpawn.rotation);
}
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
rb.velocity = new Vector3 (moveHorizontal, 0.0f, moveVertical) * speed;
rb.position = new Vector3 (
Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler (0, 0, rb.velocity.x * -tilt);
}
}
Shot uses Mover script:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
private Rigidbody rb;
public float speed;
void Start() {
rb = GetComponent<Rigidbody> ();
rb.velocity = new Vector3 (0.0f, 0.0f, 1.0f) * speed;
}
}
EDIT Seems like I've got it to work. I had set the x rotation on the on the shot quad to 90 but not the x rotation on the shot spawn. PING @AlphaBigWood
Thank you, @AceWeston! I was having the exact same problem and this solved it. :)
Your answer
Follow this Question
Related Questions
Projectiles trigger affecting other projectiles of same prefab - C# 0 Answers
Object created through Instantiate does not update its position at the first frame 1 Answer
Animation doesn't affect the new instantiated object. 0 Answers
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Instanciate prefab responsively 1 Answer