Unity clone won't appear - c#

I'm new to coding and i'm trying to spawn a clone of a ball in unity, but it won't appear.
It does spawn at the spawn point but it doesn't appear.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject ball;
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
// Update is called once per frame
void Update()
{
}
void SpawnBall()
{
Instantiate(ball, spawnPoint.position, Quaternion.identity);
}
}
Edit: it's a 3d game and the mesh renderer is enabled.

Okay nevermind: i just redid the ball and it worked.
¯_(ツ)_/¯

Related

How to increase score when clone prefabs touch wall?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountScoreAtWall : MonoBehaviour
{
//Varriables
public GameObject[] prefabs;
public Text animalPassedText;
public int animalPassed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
animalPassedText.text = animalPassed.ToString();
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == tag)
{
animalPassed = animalPassed + 1;
Debug.Log("Animal Passed showed");
}
}
I want to let my clone Prefabs from Instantiate() touch the wall and then the score will Increase by 1 to animalPassedText but I don't know how to do that.
So please help me. ToT
Possible solutions:
1- Either the wall or your prefab should have a rigidbody to make OnTriggerEnter work. So check if one of your game objects has a rigidbody and they both have colliders.
2- Maybe your "tag" variable is not matching with collision.gameObject.tag, so if block is not triggered. Print collision.gameObject.tag and tag to see if they are matching.

Unity 2D: Making the Player Gameobject Reactivate after being turned off

I can't get the Player Gameobject to reappear. The player deactivates the moment the Timeline starts when they touch the box collider, but the player never reactivates. I have tried using Player.SetActive(true) in the coroutine and even using an Invoke method with no luck. Any ideas on how to fix this? Please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class TimelineTrigger : MonoBehaviour
{
// calling items for Unity
public PlayableDirector timeline;
public GameObject Player;
public GameObject CutsceneCollider;
public GameObject CutsceneMC;
// Start is called before the first frame update
void Start()
{
// calls the playable director and turns off the MC for the scene
timeline = timeline.GetComponent<PlayableDirector>();
CutsceneMC.SetActive(false);
}
void Update()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
private void EnableAfterTimeline()
{
Player = GameObject.FindGameObjectWithTag("Player");
Player.SetActive(true);
}
public void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
// plays the cutscene and starts the timer
timeline.Play();
Player.SetActive(false);
Invoke("EnableAfterTimeline", 18);
CutsceneMC.SetActive(true);
StartCoroutine(FinishCut());
}
IEnumerator FinishCut()
{
// once the cutscene is over using the duration, turns off the collider and the MC.
yield return new WaitForSeconds(17);
CutsceneMC.SetActive(false);
CutsceneCollider.SetActive(false);
}
}
}
The issue here is that coroutines in Unity can't be run on inactive GameObjects, so FinishCut never gets executed.
This can be worked around by having a separate MonoBehaviour in the scene to which the responsibility of running a coroutine can be off-loaded. This even makes it possible to start static coroutines from static methods.
using System.Collections;
using UnityEngine;
[AddComponentMenu("")] // Hide in the Add Component menu to avoid cluttering it
public class CoroutineHandler : MonoBehaviour
{
private static MonoBehaviour monoBehaviour;
private static MonoBehaviour MonoBehaviour
{
get
{
var gameObject = new GameObject("CoroutineHandler");
gameObject.hideFlags = HideFlags.HideAndDontSave; // hide in the hierarchy
DontDestroyOnLoad(gameObject); // have the object persist from one scene to the next
monoBehaviour = gameObject.AddComponent<CoroutineHandler>();
return monoBehaviour;
}
}
public static new Coroutine StartCoroutine(IEnumerator coroutine)
{
return MonoBehaviour.StartCoroutine(coroutine);
}
}
Then you just need to tweak your code a little bit to use this CoroutineHandler to run the coroutine instead of your inactive GameObject.
public void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
// plays the cutscene and starts the timer
timeline.Play();
Player.SetActive(false);
Invoke("EnableAfterTimeline", 18);
CutsceneMC.SetActive(true);
CoroutineHandler.StartCoroutine(FinishCut()); // <- Changed
}
IEnumerator FinishCut()
{
// once the cutscene is over using the duration, turns off the collider and the MC.
yield return new WaitForSeconds(17);
CutsceneMC.SetActive(false);
CutsceneCollider.SetActive(false);
}
}
If you have only one Player instance and it's accessible from the start, you'd better set it once in the Start method.
void Start()
{
// calls the playable director and turns off the MC for the scene
timeline = timeline.GetComponent<PlayableDirector>();
CutsceneMC.SetActive(false);
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
}
private void EnableAfterTimeline()
{
Player.SetActive(true);
}

destroying gameObject in both cases in Unity

I have written a code where when the player is not being rendered in camera it should be destroyed but it is being destroyed even being rendered in camera, please see my below code;
using UnityEngine;
public class IfnotvisibleDestroy : MonoBehaviour
{
public SpriteRenderer re;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void Update()
{
if (re.isVisible)
{
Debug.Log(re.isVisible);
}
if(!re.isVisible)
{
Destroy(gameObject);
}
}
}
Do the destroy gameObject part inside the FixedUpdate function

Unity C# problem with camera, camera goes white

I made a script for my camera for following the player.When I play the game, game view goes white, even if on scene view all is fine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos;
}
}
The problem might be that the player is blocking the camera (because the camera is inside the player). Try adding some offset by adding a Vector3 as a variable and adding it on to the transform.position.
The offset could be used so that the camera is in front of the player, or in a third person angle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 playerpos;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerpos.x = player.position.x;
transform.position = playerpos + offset;
}
}
Hope this helps.

How to enable audio source in Unity?

I am trying to add some sound to my unity practice project and I have applied the sound and prepared the script necessary for it... but now whenever I am testing it through Unity player audio is not working.. each time I am getting this error log.
"Can not play a disabled audio source UnityEngine.AudioSource:Play()
coinhandler:OnCollisionEnter2D(Collision2D) (at
Assets/Script/coin/coinhandler.cs:24)"
My Coinhandler.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class coinhandler : MonoBehaviour {
public Transform particles;
public AudioSource coinCollectS;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D collider)
{
if (collider.gameObject.tag == "Player")
coinCollectS.Play ();
{
Instantiate (particles, new Vector3 (transform.position.x, transform.position.y, -0.2f), Quaternion.identity);
shooterMain.IncreaseSpeed ();
shooterMain.increasePoint ();
GameObject.FindWithTag ("points").GetComponent<Text>().text = shooterMain.getPoints ().ToString ();
Destroy (gameObject);
}
}
}

Categories