- Home /
How can I use SetActive to set something active when colliding with something?
I am trying to get coins that were previously picked up to reappear when the player dies after colliding with spikes which kill the player(handled by another script). Help would be appreciated, thanks in advance.
script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CoinPickup : MonoBehaviour {
public int pointsToAdd;
void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<PlayerController>() == null)
return;
ScoreManager.AddPoints(pointsToAdd);
gameObject.SetActive(false);
if (other.gameObject.name == "spikes first")
gameObject.SetActive(true);
}
}
Answer by Vilhien · Jul 18, 2018 at 07:23 PM
Right now from the look of it you've got this script setup on the coins themselves.
The collision for the player would make them setactive false, however..
the second if is saying if the coins collide with spikes first? If I'm reading this correct, you want that part of the script to be called from the player (more importantly checking for his collider hitting the spikes), and the gameObject set to a list (setting to active)..
Then when the player triggers set the whole list active.
Perhaps setup a tag for the coins, when triggered it makes every object with the tag setactive.
Call this in start after setting the objects to a tag you create called "Coins"
in start: coins = GameObject.FindGameObjectsWithTag("Coins");
Then set the setActive to happen for all objects with such.
You can find a similar setup here. Example here.
https://answers.unity.com/questions/429701/how-to-set-gameobjectfindgameobjectswithtag-to-set.html
I know this is a very noob question but what type of variable would it be? I.e. float, int ect.
for the coins, keep everything the same, but get rid of the part with the spikes.
For the player,
at the top it's going to need a gameObject[] coins;
that's a gameObject list called coins.
Then in start you'll need that part mentioned above. coins = GameObject.FindGameObjectsWithTag("Coins");
that would make the script on the player find all the coins in the start and add them to a list, as long as their tag is set to coins.
then on the same sort for the player,...
on void triggerenter2D(collider other) { if(other.tag="Coins") { foreach(gameObject go in Coins) {go.setActive(true); } }
Or something close to the effects.. Sorry I'm at work.. I can't get you specific code.
Thank you so much I just tried it and it worked I had spent weeks trying to figure this out on my own to no avail. Again thank you.
No problem..
And yes that happens BUT! and this is a big but..
Its GREAT when you get stuff to work on your on by trouble shooting, it instills confidence to be able to take on larger ideas and problems. I didn't know anything about unity a year ago, and like you I've struggled with many problems for weeks.. One thing I may suggest that I've read, and I try to follow.
Try to accomplish one thing a day for your game, albeit small or something you just think up. Here's the kicker, don't get stuck on something for too long. Projects like thoughts from the above can turn into a few days, weeks..
Next thing you know your trying to adjust spawn values for planets where their scale accidentally overlap suns, "gravity" (and I stress the " 's because my gravity is made up), and ter$$anonymous$$al velocity in 3D. But remember to try not to get stuck, because then its not as much fun. I've rambled too long. ~Back to figuring out planet spawn ranges from suns.
Your answer
Follow this Question
Related Questions
Account Activation 0 Answers
Manual/Offline Activation of Unity 3.x 12 Answers
Unity won't open, or go past blank license screen 0 Answers
is it Possible to Move a UI Slider remotely by script? If So How 1 Answer
How do I acitvate Unity? 0 Answers