Multiple instance of Game Object while setting Game object Active - c#

I am newbie to Unity 3D. I have created a game object(A missile) at position x:-1 y:-3 z:0 Added a component script which moves the ball in upward direction. Saved it as a prefab. Then deleted the Game object from the scene.
Component Script used
using UnityEngine;
public class Missile : MonoBehaviour
{
[SerializeField] protected Rigidbody2D rb;
public float speed = 4;
void Start()
{
Invoke("Object", 2.0f);
}
void Object()
{
float x = Random.Range(-2.0f, 2.0f);
float y = -5.0f;
rb.MovePosition(new Vector3(x, y, 0));
Debug.Log("Position of Rigid Body: " + rb.position);
rb.velocity = Vector2.up * speed;
}
virtual protected void Die()
{
Destroy(gameObject);
}
}
Invoked a script which takes prefab as input parameter and spawns the Game object at random position & moves up as per the script component of missile. I need the missile to trigger at random location, but the missile is getting triggered both at position x:-1 y:-3 z:0(Game object position) & some random location. How can I avoid the Game object position while spawning
Script used to invoke the prefabs
using UnityEngine;
using System.Collections;
using TMPro;
public class MissileSpawnner : MonoBehaviour
{
[SerializeField] GameObject[] missilePrefabs;
[SerializeField] int missileCount;
[SerializeField] float spawnDelay;
GameObject[] missiles;
#region Singleton class: MissileSpawnner
public static MissileSpawnner Instance;
void Awake()
{
Instance = this;
}
#endregion
void Start()
{
PrepareMissiles();
StartCoroutine(SpawnMissiles());
}
IEnumerator SpawnMissiles()
{
for (int i = 0; i < missileCount;i++)
{
Debug.Log("Object set as active");
missiles[i].SetActive(true);
yield return new WaitForSeconds(spawnDelay);
}
}
void PrepareMissiles()
{
missiles = new GameObject[missileCount];
int prefabsCount = missilePrefabs.Length;
for (int i = 0; i < missileCount; i++)
{
missiles[i] = Instantiate(missilePrefabs[Random.Range(0, prefabsCount )]);
missiles[i].SetActive(false);
}
}
}

Change this:
missiles[i] = Instantiate(missilePrefabs[Random.Range(0, prefabsCount )]);
to
missiles[i] = Instantiate(missilePrefabs[Random.Range(0, prefabsCount )], <vector3position>, <quaternionrotation>);
and you will specify its position and rotation, just replace vector3position with a vector3 of your choosing and quaternionrotation with a quaternion rotation of your choosing. You can use Quaternion.Identity if you don't want any rotation. You can also add a parent if you choose as well.
missiles[i] = Instantiate(missilePrefabs[Random.Range(0, prefabsCount )], <vector3position>, <quaternionrotation>, <transformparent>);
Just replace transformparent with the gameobject you want to be the parent of the bullet, if this override can prove useful to you.
More reading on Object.Instantiate
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Related

How to generate ground in unity2d endless runner

I currently write a game, and I have 2 scripts that generate the ground in the game. However, instead of generating them as the player comes to the end of one ground, they're generated as soon as the game starts. I don't want this to happen.
Does someone know why it happens?
Please help me fix this.
Thanks!
This is my code:
Script 1:
public class ObjectPooler : MonoBehaviour
{
public GameObject pooledObject;
public int pooledamnt;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
pooledObjects = new List<GameObject>();
for (int i = 0; i < pooledamnt; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
// Update is called once per frame
void Update()
{
}
}
Script 2:
public class
GroundGenerator : MonoBehaviour
{
public GameObject thePlatform;
public Transform GenOnPoint;
public float DistanceBetween;
private float PlatformWidth;
public float DistanceBewtweenmin;
public float Di stanceBetweenmax;
public ObjectPooler objectpool;
public GameObject[] thePlatforms;
private int platformSelecter;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject newPlatform = objectpool.GetPooledObject();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive(true);
}
}
Here is a basic script to generate ground as the player moves :
using UnityEngine;
public class GroundGeneration : MonoBehaviour
{
public float ClosestDistacnceFromPlayer;
public GameObject GroundTile;
public float TileWidth;
public Transform Player;
void Start()
{
//Spawn a tile so that the player won't fall off at the start
SpawnTile(1);
}
void SpawnTile(int n)
{
int i = 0;
//Spawn n tiles
while (i < n)
{
Instantiate(GroundTile, transform.position, Quaternion.identity);
i++;
//Teleport the "Ground generator" to the end of the tile spawned
transform.position += TileWidth * Vector3.right; // Or use Vector3.forward if you want to generate ground on z axis.
}
}
void FixedUpdate()
{
if (Vector3.Distance(Player.position, transform.position) <= ClosestDistacnceFromPlayer)
{
SpawnTile(1);
}
}
}
The GroundTile should have its origin at the point where it meets with the previous tile and it should be a prefab.
The Ground will start generating at the position of the object containing the ground generation script.

Changing data on click button

I have some functionalities within some GameObjects.
These functionalities need to be changed when an upgrade is purchased within the game. The problem is that each function is set on its own object.
The first problem is that the variables don't change when I click on the button. As you can see I have added an onclick value to the button, stating that when the button is clicked. The value should change.
The problem here is that "Object reference not set to an instance"
The second problem I face is that each projectile is fired independently. So if I change the static damage of 1 it won't be transferred to other projectiles.
UpgradeMenu
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UpgradeMenu : MonoBehaviour
{
[SerializeField]
private Text accuracyText;
[SerializeField]
private Text speedText;
[SerializeField]
private Text damageText;
[SerializeField]
private float accuracyMultiplier = 0.7f;
private Weapon weapon;
private Projectile projectile;
private Player player;
void OnEnable()
{
UpdateValues();
}
void UpdateValues ()
{
accuracyText.text = weapon.randomAngle.ToString();
damageText.text = projectile.DamageOnHit.ToString();
speedText.text = player.MaxRun.ToString();
}
public void UpgradeAccuracy ()
{
weapon.randomAngle = (int)weapon.randomAngle * accuracyMultiplier;
UpdateValues();
}
public void UpgradeDamage ()
{
projectile.DamageOnHit = (int)projectile.DamageOnHit + 1;
UpdateValues();
}
}
Projectile (DamageScript)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//[RequireComponent (typeof(Rigidbody2D))]
public class Projectile : MonoBehaviour {
[Header ("Speed")]
public float baseSpeed;
public float randomSpeed;
public Vector2 SpeedV2;
public Vector2 Direction;
[Header ("Damage")]
public int DamageOnHit;
[Header ("Layers")]
public LayerMask solid_layer;
public LayerMask entities_layer;
[Header ("OnHit FX")]
public GameObject HitFxPrefab;
public GameObject DustFxPrefab;
[Header ("Bounce")]
public bool BounceOnCollide = false;
public int bouncesLeft = 0;
[HideInInspector]
public Health owner; // owner of the projectile
private Vector2 Position; // Current position
private Vector2 movementCounter = Vector2.zero; // Counter for subpixel movement
public BoxCollider2D myCollider;
List<Health> healthsDamaged = new List<Health>(); // List to store healths damaged
void OnCollideWith (Collider2D col, bool horizontalCol = true) {
var component = col.GetComponent<Health> ();
// If the target the hitbox collided with has a health component and it is not our owner and it is not on the already on the list of healths damaged by the current hitbox
if (component != null && component != owner && !healthsDamaged.Contains(component)) {
// Add the health component to the list of damaged healths
healthsDamaged.Add (component);
// Apply the damage
var didDamage = component.TakeDamage (DamageOnHit);
// Destroy the projectile after applying damage
if (didDamage) {
DestroyMe ();
return;
}
}
// if the projectile hit's a solid object, destroy it
if (col.gameObject.layer == (int)Mathf.Log(solid_layer.value, 2)) {
DestroyMeWall ();
return;
}
}
void OnCollideWithEntity(Collider2D col) {
var component = col.GetComponent<Health> ();
// If the target the hitbox collided with has a health component and it is not our owner and it is not on the already on the list of healths damaged by the current hitbox
if (component != null && component != owner && !healthsDamaged.Contains(component)) {
// Add the health component to the list of damaged healths
healthsDamaged.Add (component);
// Apply the damage
var didDamage = component.TakeDamage (DamageOnHit);
// Destroy the projectile after applying damage
if (didDamage) {
DestroyMe ();
}
}
}
First of all, change
[Header ("Damage")]
public int DamageOnHit;
to static
public static int DamageOnHit = /*your starting value*/;
This ensures that all projectiles will share the same damage it deals on a hit.
For instance, if you currently have 10 projectiles in a scene, and DamageOnHit is 2, they all will deal 2 damage.
Without the static, each of the projectile will have it's own DamageOnHit. This brings us to the next case too:
If each projectile had it's own DamageOnHit, and we want to modify DamageOnHit, we need to specify which projectile's damage to modify.
But if it's static, it becomes much simpler as ALL of the projectile shares the same DamageOnHit.
Now, if you wanted to change the DamageOnHit for ALL projectiles, just do
Projectile.DamageOnHit = /*Your new damage value*/
Also, your null reference exception occured due to the fact that you never did assign your projectile in UpgradeMenu.
(Notice how you never did projectile = /*your projectile*/ in UpgradeMenu.cs?)
By default, that will make the variable null. And trying to do null.DamageOnHit += 1 would make no sense.
Small Edit: Making a variable static would also mean that you can't expose it to the inspector. But you can assign a starting value like the code shown initially.

OnCollisionEnter not working Unity3D

I'm trying to build a game where u need to dodge falling objects. I've made a hazard but it seems as if the hazard 'clone' is behaving diffrently.
I've made a collision script when the hazard hits the platform it needs to disappear. This works for the hazard object, but not the hazard clone objects that fall.
As u can see in the first screenshot, the red circled block behaves
like it use to. But the blue circled once (clones) fall right through
objects.
As u can see in the second screenshot, the red circled one is gone,
because it hit the platform. But still the blue once fall right
through.
Thanks in advance!
Below u will find the Collision script, below that is the Hazard Spawn script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HazardCollisionFunctions : MonoBehaviour {
#region Variables
//Public
//Private
#endregion
#region UnityFunctions
void Start()
{
}
void Update()
{
}
#endregion
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "platform")
{
this.gameObject.SetActive(false);
}
if(collision.gameObject.tag == "Player")
{
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnHazards : MonoBehaviour {
#region Variables
//Public
//Private
[SerializeField]
public float minX = 0.0f;
[SerializeField]
public float maxX = 0.0f;
[SerializeField]
private GameObject[] hazards; //potential array of hazards
[SerializeField]
private float timeBetweenSpawns = 0.0f;
private bool canSpawn = false;
private int amountOfHazardsToSpawn = 0;
private int hazardToSpawn = 0;
#endregion
#region UnityFunctions
public void Start()
{
canSpawn = true; //Temp start
}
public void Update()
{
if(canSpawn == true)
{
StartCoroutine("GenerateHazard");
}
}
#endregion
private IEnumerator GenerateHazard()
{
canSpawn = false;
timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing values
amountOfHazardsToSpawn = Random.Range(1, 5); //Testing values
for(int i = 0; i < amountOfHazardsToSpawn; i ++)
{
Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), 8.0f, 0.0f); //Gen spawnpoint for the hazard
Instantiate(hazards[hazardToSpawn], spawnPos, Quaternion.identity); //Spawn the hazard
}
yield return new WaitForSeconds(timeBetweenSpawns);
canSpawn = true;
}
}
OnCollisionEnter
OnCollisionEnter takes Collision object as a parameter and it requires the isTrigger property of the attached Collider component to be FALSE.
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}
OnTriggerEnter
OnTriggerEnter takes Collider object as a parameter and it requires the isTrigger property of the attached Collider component to be TRUE.
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("CheckPoint"))
{
Destroy(other.gameObject);
}
}
If you are instantiating the object from prefab, make sure that
prefab have required components (rigidbody/collider) and
properties to achieve the desired behaviour.
To detect Collision/Trigger, at least one of the object must have a
physics component (Rigidbody)
Rigidbody MUST be attached to the moving object.
Hope this helps :)

How can i make that the user can select if the camera will be behind the player or not?

This is the original code make the camera follow the player:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - objectToFollow.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = objectToFollow.transform.position + offset;
transform.LookAt(objectToFollow.transform);
}
}
And this is what i tried to do but then the player it self(ThirdPersonController) is not rotating according to where he move.
With the original script above he does.
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
public bool behindPlayer = false;
private Vector3 cameraStartPos;
// Use this for initialization
void Start()
{
cameraStartPos = transform.position;
// Put the camera behind the player
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
}
private void Update()
{
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
else
{
transform.position = cameraStartPos;
}
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
transform.position = objectToFollow.transform.position;
transform.LookAt(objectToFollow.transform);
}
}
I want that the camera will be automatic behind the player already without the need to change the camera position in the scene if the user want by using the bool variable.
But the script now is not like in the above:
With this line in the LateUpdate:
transform.position = objectToFollow.transform.position;
Using the bool false/true it's not changing the camera position at all.
Without this line the player will move and the camera will follow but the player will not rotate and not move as above.
What i want is just like the first script but with the behindPlayer variable.
A working script:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void Update()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
In the inspector set the offset for example to 0,5,-12 and as target set for example ThirdPersonController. And attach the script to the Camera(Camera must be tagged as MainCamera if the target is ThirdPersonController since the ThirdPersonController ThirdPersonUserControl script is looking for the MainCamera).
And the camera doesn't have to be a child of the target the camera can be in any place in the Hierarchy.
Anyway it's working.

How to get a projectile to shoot towards a gameobject

Im currently working on a TurretAi. I have it so that when the enemy is within a certain range the turret targets the enemy but I'm unable to get the turret to shoot the projectiles toward the enemy. this is currently what i have this is turret class.
using UnityEngine;
using System.Collections;
public class Defence : MonoBehaviour {
public float DistanceFromCastle,CoolDown;
public GameObject enemy;
public GameObject Bullet;
public int protectionRadius,bulletSpeed;
// Use this for initialization
void Start ()
{
protectionRadius = 35;
bulletSpeed = 50;
CoolDown = 5;
}
// Update is called once per frame
void Update () {
enemy = GameObject.FindGameObjectWithTag("Enemy");
if(enemy != null)
{
DistanceFromCastle = Vector3.Distance(GameObject.FindGameObjectWithTag("Enemy").transform.position,GameObject.FindGameObjectWithTag("Defence").transform.position);
//print (DistanceFromCastle);
if(DistanceFromCastle <= protectionRadius)
{
attackEnemy();
}
}
}
void attackEnemy()
{
transform.LookAt(enemy.transform);
CoolDown -= Time.deltaTime;
if (CoolDown <= 0)
{
Debug.DrawLine(transform.position,enemy.transform.position,Color.red);
Instantiate(Bullet,Vector3.forward,Quaternion.identity);
print("attack Enemy");
CoolDown = 5;
}
}
}
I also already have a cool down var so that it only shoot every 5 second any help would be awesome.
You were fairly close, you need to change this line:
Instantiate(Bullet, Vector3.forward, Quaternion.identity);
To this:
private const int SPAWN_DISTANCE = 5;
Instantiate(Bullet, transform.position + SPAWN_DISTANCE * transform.forward, transform.rotation);
Quaternion.identity refers to:
This quaternion corresponds to "no rotation".

Categories