I made 2 scripts and assigned it to 2 cubes: "cube" and "cube 1".
Normally if you click on cube it sets a value so that when you click cube 1 it disappears.
If you click cube 1 first it's not gonna work.
So that's what I tried to make but it does not work and I don't understand why.
here are my scripts
cube:
using UnityEngine;
using System.Collections;
public class script : MonoBehaviour
{
public int test = 0; // make the variable for the value
public void OnMouseDown() // when the user click
{
test = 1; //make the value of test 1
}
}
cube 1:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour
{
public GameObject a; //make a gameobject
public script script; //make a variable where we put in the script
void OnMouseDown() // when the user click
{
script = a.GetComponent<script>(); // get script
if ( script.test == 1) //test or the variable test in the other script is 1
{
Destroy(gameObject); // destroy the object
}
}
}
Can someone please help me?
Change the name of the script class to be capitalised.
public class Script : MonoBehaviour
Then in the NewBehaviourScript1 change everything inside it to:
public class NewBehaviourScript1 : MonoBehaviour
{
public Script script; //Drag the other cube onto this in the inspector.
void OnMouseDown()
{
if ( script.test == 1)
{
Destroy(gameObject);
}
}
}
You should use more descriptive names for both your classes and their instances.
Note: For this to work you will have to have drag the other cube onto the script variable in the inspector and it will have to have a Script attached to it.
Try using a.GetComponent<script>();. You need to pass a Type to GetComponent in order for it to work. I'm not sure that your code even compiles, its very hard to read it because you did not format it properly.
Related
I need to destroy an object, in my case is "Player2" witch is in the same scene as "Player1" and then after Player1 position.x is past "baraj".position.x reapear. Basically how can i make a temporarly destroy method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class switchCameras2 : MonoBehaviour
{
public GameObject baraj;
public GameObject CameraOne;
public GameObject CameraTwo;
public GameObject Player1;
public GameObject Player2;
public void Start()
{
CameraOne.SetActive(true);
CameraTwo.SetActive(false);
}
void Update()
{
if (GameObject.Find("Player1").transform.position.x > GameObject.Find("baraj").transform.position.x)
{
CameraTwo.SetActive(true);
CameraOne.SetActive(false);
}
if (GameObject.Find("Player1").transform.position.x < GameObject.Find("baraj").transform.position.x)
{
Destroy(Player2);
}
if (GameObject.Find("Player1").transform.position.x > GameObject.Find("baraj").transform.position.x)
{
Destroy(Player1);
}
}
}
If you want to Destroy it then you should make a new one with Instantiate method. If you don't want to Destroy it, one alternative is to deactivate it with SetActive like this Player1.SetActive(false); and then enable it like this Player1.SetActive(true);
Try creating a new GameObject variable called "U". And then right before destroying or in the start do U = player2.gameObject;
Now let's say you destroyed it and want to spawn. To do that we do
player2 = U.gameObject;
Instantiate(player2);
That will spawn player 2.
But as mentioned in the other answer .SetActive(false); would make things easier. Anyway if you still want to use destroy then that is how to do that. Good luck
I try to create a shop system where you can buy a character to play with. When I buy a character in the Shop Scene, the bool value in the Inventory script is set to true. When I switch to the Level1 Scene, my character is not spawned. I suppose there is a bug in my GameManager script but I can not find it. Can you please help me? I do not know any more.
GameManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject Joli;
public GameObject Ninn;
public GameObject Spaci;
public GameObject Woodie;
public GameObject Plumbt;
public GameObject canvasObject;
public Inventory myInventory;
// Start is called before the first frame update
void Start()
{
myInventory = GameObject.FindObjectOfType<Inventory>();
if (myInventory.GetNinn() == true)
{
GameObject Ninnnew = Instantiate(Ninn, Ninn.transform.position, Quaternion.identity) as GameObject;
Ninnnew.transform.SetParent(canvasObject.transform, false);
}
if (myInventory.GetSpaci() == true)
{
GameObject Spacinew = Instantiate(Spaci, Spaci.transform.position, Quaternion.identity) as GameObject;
Spacinew.transform.SetParent(canvasObject.transform, false);
}
}
}
There is currently not enough information / code in your question, but when you change scene, your objects are destroyed by default, and new ones are isntantiated, effectively resetting any values that are set in them.
You need to prevent your Inventory or any object holding data you need to keep that it should not be destroyed when switching scenes, by using DontDestroyOnLoad for instance :
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
I'm making some sort of a Evolution simualtor game. I have a script that is supposed to destroy the GameObject it's attached to when a creature's CapsuleCollider Triggers the OnTriggerEnter().
I have a problem that even tho the Creature's collider isn't even close to the Food, it still destroys the GameObject.
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FoodEat : MonoBehaviour
{
public GameObject FoodGO;
public Rigidbody FoodRB;
private void OnTriggerEnter(Collider Creature)
{
Destroy(FoodGO);
}
void Start()
{
FoodRB = GetComponent<Rigidbody>();
FoodGO = FoodRB.gameObject;
}
void Update()
{
Rigidbody[] allRigidBodies = (Rigidbody[])FindObjectsOfType(typeof(Rigidbody));
foreach (Rigidbody body in allRigidBodies)
{
if (body.gameObject.layer == 10)
{
OnTriggerEnter(body.gameObject.GetComponent<CapsuleCollider>());
}
}
}
}
OnTriggerEnter is a monobehaviour lifecycle method. You should not call it from your own code; it will automatically be called when it detects collisions.
Furthermore, the logic in your code right now seems to be incorrect, it is...
"Every frame, loop through all rigidbodies in the scene and if 1 is found on layer 10, destroy the FoodGO"
Simply remove your entire Update method and put an if in your Collision method, and it should work:
[RequireComponent(typeof(Rigidbody), typeof(Collider))]
public class FoodEat : MonoBehaviour
{
private void OnTriggerEnter(collider other)
{
Debug.Log(other.gameObject.name + " on layer " + other.gameObject.layer);
if (other.gameObject.layer == 10)
Destroy(this.gameObject);
}
}
A few noteworthy edits of your code:
I removed FoodGO, since it's the GameObject this script is attached to, you can access it by just writing gameObject or this.gameObject.
I remove the Rigidbody reference since it is not used anymore, and thus the entire Start() method.
Since this code requires a Rigidbody and a Collider to work, I added a [RequireComponent] attribute in the top, which will make Unity tell you if you forgot to add those components on the object you attach this script to.
I added a Debug.Log that prints the name & the layer on the creature that collides with the food, so you can debug and make sure it is working as expected
I am just trying to activate game over screen when the Player's activate is 'false'. There is no animation, just the
There are 3 objects that need to be active and i added the script to those 3 objects but the screen does not appear.
How can i fix?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
if (_player.activeInHierarchy == false)
{
gameObject.SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
}
My suspicion is the following. Whenever a Gameobject is not enabled, its code does not run. Test this by adding a Debug.Log("test") message.
If no message appears you can be certain that this check is never evaluated. To work around this simply add a script that is bound to an active gameObject. Creat a new empty gameobject in the scene. And add something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
public GameObject _endscreen;
void Update()
{
if (_player.activeInHierarchy == false)
{
_endscreen.SetActive(true);
}
else
{
_endscreen.SetActive(false);
}
}
}
Assign the Variables in the Inspector by dragging the object to the empty fields. Never use GameObject.Find Methods.
If you need any further help tell me :)
What you're currently doing is to find a single GameObject and checking whether that is active or not.
It would make more sense, and be more optimized, if each player object adds himself to a list of all players when he spawns. You can then loop over that list instead to check all players.
An even better way would be if you only call that "GameOver" check after a player has died. So when you call whichever method kills him.
Based on Franz Answer, I did some modification in the code. You won't need any if else statement if you use a shortcut.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
public GameObject _endscreen;
void Update()
{
_endscreen.SetActive(!_player.activeInHierarchy);
}
}
I'm making a game in Unity3D and I have a 2 different scripts. One called Rays (it checks what I'm clicking on and lowers its hp) and a script called colorChange (it changes the color of the object that I clicked on depending on its hp). I created the hp variable in colorChange and I need to check hp in Ray.
So the "colorChange" script depends on the "Rays" script, yes?
Then you can define the "colorChange" script to expect a "Rays" component on the same GameObject by using the [RequireComponent] tag, which is described
here: https://docs.unity3d.com/ScriptReference/RequireComponent.html
Then in the "Awake" function of "colorChange" you retrieve a reference to "Rays". If the "hp" variable in "Rays" has public get access then in "colorChange" you can use the acquired reference to the "Rays" script to check its current value.
Example for script "Rays":
using UnityEngine;
public class Rays : MonoBehaviour {
private int hp = 0;
public int Hitpoints {
get { return hp; }
}
// ... other methods ...
}
Example for script "colorChange":
using UnityEngine;
[RequireComponent (typeof (Rays))]
public class colorChange : MonoBehaviour {
private Rays raysReference = null;
protected void Awake() {
raysReference = GetComponent<Rays>();
}
protected int getRaysHitpoints() {
return raysReference.Hitpoints;
}
// ... other methods that may use getRaysHitpoints ...
}