Can' instantiate a prefab in a certain time - c#

I'm trying to instantiate a prefab in a certain time, but that gives me an error: "object reference not set to an instance of an object".
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class animatronicsManager : MonoBehaviour
{
clockTime clockTime;
public GameObject enemy;
void Start()
{
clockTime.FindObjectOfType<clockTime>();
}
void Update()
{
if (clockTime.time2 == 10)
{
Instantiate(enemy, new Vector3(3.66f, -1, 0.66f), Quaternion.Euler(0, -190, 0));
}
}
}
i also tryed to to spawn it by pressing a key, and it works. Can you help me resolve this?
("time2" is an int variable that count time)
Thank you

clockTime variable is never initialized in your code. This is the reason of the error. Initialize it in in your Awake() (or anywhere before trying to access in the Update() method)

I suppose you wanted your Start to be like this
void Start()
{
clockTime = FindObjectOfType<clockTime>();
}

Related

How to reference an Object in Unity with C#?

Hy Guys
I'm getting the error CS0120 in Unity at following Code:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PortalScript.Spawn()'
Script 1: here I try to create a new GameObject on Screen with a certain distance to Player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PortalScript : MonoBehaviour
{
public GameObject Portal; // referenced at Inspector
GameObject Player = GameObject.Find("Player");
public void Spawn()
{
bool portalSpawned = false;
while (!portalSpawned)
{
Vector3 portalPosition = new Vector3(Random.Range(-7f, 7f), Random.Range(-4f, 4f), 0f);
if((portalPosition - Player.transform.position).magnitude < 3)
{
continue;
}
else
{
// Instantiate at position.
Instantiate(Portal, portalPosition, Quaternion.identity);
portalSpawned = true;
}
}
}
}
Script 2: This script is on the Player. On Case it should call the method Spawn from script 1
public class Point : MonoBehaviour
{
public PortalScript Spawn;
void Update()
{
score = updateScore;
switch (score)
{
case 1:
PortalScript.Spawn(); // ERROR at this line
break;
}
}
If I write the code from script 1 directly into script 2, it works.
My brain stops at that point. Thanks for all your help and let me know if you need more information.
Replace this line:
PortalScript.Spawn(); // ERROR at this line
with this:
Spawn.Spawn();

How to properly make variable Public so it can be accessed by another script?

In Unity I have 2 GameObjects, a sphere and a capsule.
And I have a script attached to each.
Capsule script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh capsuleMesh;
void Awake()
{
capsuleMesh = GetComponent<MeshFilter>().mesh;
Debug.Log(capsuleMesh);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Sphere script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
Mesh mesh;
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
Debug.Log(mesh);
}
// Start is called before the first frame update
void Start()
{
mesh = capsuleMesh;
}
// Update is called once per frame
void Update()
{
}
}
The mesh = capsuleMesh here is giving me an error about "the name capsuleMesh does not exist in the current context".
I thought that making capsuleMesh public in the other script would make THIS script be able to access it without issue.
What am I doing wrong?
capsuleMesh is a class variable defined in the CapsuleMesh class. It's not a global variable you can use everywhere. You need a reference to the instance of the CapsuleMesh class to retrieve the mesh stored in the capsuleMesh variable.
I've reworked your both scripts to make them work. I've spotted a flaw in your scripts. I guess ChangeMesh is meant to change the mesh of the gameObject? If so, you need to assign a new value to the meshFilter.mesh. Assigning a new reference to the mesh class variable is not enough (it would be pretty long to explain why)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh Mesh
{
get ; private set;
}
void Awake()
{
Mesh = GetComponent<MeshFilter>().mesh;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
// Drag & drop in the inspector the gameObject holding the `CapsuleMesh` component
public CapsuleMesh CapsuleMesh;
private MeshFilter meshFilter;
void Awake()
{
meshFilter = GetComponent<MeshFilter>();
}
void Start()
{
meshFilter.mesh = CapsuleMesh.Mesh;
}
}

How could i fix an error (cs0120) in unity?

In unity, I'm trying to make a press of a button increase the speed of the player. However, each time I run it. It gives me:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PlayerController.speed'
I've already tried changing order of the code, so what could I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Upgrader1 : MonoBehaviour
{
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
public class Upgrader1 : MonoBehaviour
{
PlayerController PlayerController; //It should be member variable
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
it's always a good thing to use proper naming conventions.
PlayerController _PlayerController;
void Start() {
GameObject Player = GameObject.Find("Player");
_PlayerController = Player.GetComponent<PlayerController>();
}
public void UpgradeSpeed() // I changed the name according to its functionality
{
_PlayerController.speed++;
}
With this, you won't put PlayerController class reference again by mistake.

TextMeshPro Text not changing, console is giving rapid errors

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Num : MonoBehaviour
{
private int score;
public TextMeshPro TMP;
void Start()
{
TMP = GetComponent<TextMeshPro>();
score = 0;
}
void Update()
{
TMP.text = score.ToString();
score++;
}
}
The text is not changing, and I dont know why. The error in the console is "NullReferenceException: Object reference not set to an instance of an object
Num.Update () (at Assets/Scripts/Num.cs:19)"
The error is that your script isn't finding a TextMeshPro sibling component. If you're using the UI version, what you actually want is to find a TextMeshProUGUI
I'm guessing it's a text ui from what you said. If it is a text UI and Textmeshpro, then use TMPro.TextMeshProUGUI varName;

How can I link my C# scripts so that item count influences score in my Unity game?

I am trying to make it so that collecting an item will cause the score to increase. After perusing the documentation for Unity, I have written the following C# scripts. CollectableItem.cs is attached to the items in the game. ScoreBoard.cs is attached to the UI display. I'm getting the error message, "'ScoreBoard.score' is inaccessible due to its protection level." If I make the variables in ScoreBoard.cs public, I get a different error message: "An object reference is required for the non-static field, method, or property 'ScoreBoard.score'."
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
ScoreBoard.score += pointsValue;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreBoard : MonoBehaviour {
[SerializeField] private Text scoreLabel;
private int score;
void Start () {
score = 0;
}
void Update () {
scoreLabel.text = "Score: " + score.ToString();
}
}
UPDATE: Here is Take 2 on CollectibleItem.cs. Now I'm being informed that "board" does not exist in the current context...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void Start() {
var uiObject = GameObject.Find("Timer");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
}
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
board.score += pointsValue;
}
}
This is not able to work because you make a so-called static access to the ScoreBoard class. That means you try to change the variable of the ScoreBoard class. What you want to do is to change the variable on one of the instances. When the UI Object is created, an instance of the class of your ScoreBoard-Script is created. Like every item has its own instance of CollectibleItem.
You can get the instance in this way:
var uiObject = GameObject.Find("Name of UI Object");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
You can do this in Start() and save the ScoreBoard variable in the script where your other private variables reside and use it later in the trigger, or you can do this directly in your Trigger function and directly set the score:
board.score += pointsValue;
EDIT: You have to place the declaration of Scoreboard inside the class:
ScoreBoard board;
void Start ()
...
Or put the code from start to OnTriggerEnter.

Categories