- Home /
[solved] passing energy from one object to another
Hi guys.
I have a bunch of receptacles which I want to be able to pass energy to one another, and have their own energy fade away. I do this by having them alter the energy variables in objects around them, but because of this they keep eachother from fading out.
So I wanted to have an array which saves all the receptacles which pass energy to an object, and have that object exclude those when it passes the energy itself.
The code that I used here:
var energyfeed : float = 0.01;
private var energyOrigin = new Array();
var hitColliders = Physics.OverlapSphere(transform.position,6);
for(var i=0; i<hitColliders.Length; i++) {
if(hitColliders[i].gameObject.name=="receptacle") {
for(var o=0;i<energyOrigin.length; o++) {
if(hitColliders[i].gameObject!=energyOrigin[o]) {
var comp = hitColliders[i].GetComponent(ReceptacleBehaviour);
comp.lightEnergy+=comp.lightDecay+energyfeed;
comp.energyOrigin.Add(gameObject);
Debug.Log("check 2");
}
Debug.Log("check 1");
}
}
}
if I remove the second for loop it works with the problems mentioned above. Does anyone see my problem here? Thanks.
Edit: It's finally working :D If anyone needs the final code, here it is:
var debug : boolean = false;
var lightEnergy : float = 0;
var lightDecay : float = 0.05;
var energyfeed : float = 0.01;
private var hitColliders : Collider[];
private var passing : boolean = false;
private var cooldown : int = 0;
function Update () {
if (lightEnergy>0 && debug==false)
lightEnergy-=lightDecay;
if(lightEnergy>1)
lightEnergy=1;
if(lightEnergy<0) {
lightEnergy=0;
hitColliders = null;
cooldown = 50;
}
if(cooldown>0)
cooldown-=1;
if(cooldown==1) {
passing = false;
}
setBrightness(lightEnergy);
if(lightEnergy>0.6) {
if(passing==false) {
hitColliders = Physics.OverlapSphere(transform.position,6);
passing = true;
if(hitColliders!=null)
Debug.Log("Cupcakes");
}
}
if(passing==true && cooldown==0) {
for(var i=0; i<hitColliders.length; i++) {
var obj = hitColliders[i].gameObject;
if(obj.name=="receptacle") {
var comp = obj.GetComponent(ReceptacleBehaviour);
if(comp.passing==false) {
comp.lightEnergy+=comp.lightDecay+energyfeed;
}
}
}
}
}
PS. "SetBrightness" is just a function that adjusts some shader settings.
im not sure what the array is for. why not have a true/false variable in each of the scripts that goes on when the object is passing? then have spherecast check for it.
also, I fear spherecasting from multiple objects every frame might slow ya down.
if it only happens a few times a second ins$$anonymous$$d it pry wont effect your gameplay. i would put the spherecast into something like this:
slow=slow-1;
if(slow<1){slow=10;
put spherecast here
}
Thank for your answer! I tried that, and it made the transition a bit choppy (not unexpected). However, your true/false variable idea can definitely work, and I sort off combined both parts of your answer.
You were right about the spherecasting, by the way, but when I stopped to think a little bit I realised that none of the receptacles ever moved, so there was no reason to check for them every single frame.
So, I re-wrote the code to this: var energyfeed : float = 0.01; private var hitColliders = new Array();
if(lightEnergy>0.6) {
if(hitColliders==null)
hitColliders = Physics.OverlapSphere(transform.position,4);
for(var i=0; i<hitColliders.length; i++) {
if(hitColliders[i].gameObject.name=="receptacle") {
if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour)==null) {
var comp = hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour);
comp.lightEnergy+=comp.lightDecay+energyfeed;
}
}
}
}
Sadly I can't test this, 'cause on the "hitColliders[i].gameObject" parts it keeps telling me that gameObject isn't a member of Object. I'm not really sure how to get around this, because, as far as I knew, OverlapSphere just returns an array. The only difference I made here, as far as I can tell, is that i created the variable before having OverlapSphere write to it. Anyway, I'll let you know how well this works once I solve that little problem. Thanks for your reply :)
Edit: I was a bit dumb. Changed "private var hitColliders = new Array();" to "private var hitColliders : Collider[];" and that got rid of the error. I also changed "if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour)==null)" to "if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour).hitColliders==null)" It's still not perfect though, 'cause now the second receptacle is just flashing. It seems I'm going to have to make another boolean anyways.
After a lot of tinkering it's working and running smoothly :D Thanks for the comment that helped me get there eventually :P
I'll put the updated, full Update() code in the original comment in case anyone needs this in the future... for whatever reason.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i make a loop for a boolean? 2 Answers
What am I overlooking? -1 Answers
how to find if all objects in array are missing 1 Answer
Problem with nested for loops 2 Answers