So i have a game concept where in the 2d game you shoot a flare gun and the ennemies follow the flare bullet, the problem is when you shoot the bullet, the ennemies dont follow it but they follow the imaginary position that the bullet prefab has, the prefab isnt even in the hirearchy.
You need to reference the spawned gameobject in some way via code. You canāt just drag on the prefab and it will work after instantiating, since instantiate will make a copy.
The prefab (or instanced object) is always in the hierarchy as long as it is spawned properly.
Well how do i refference the spawned gameobject via code, could i use tags ore something like that?
if you just need the object itself, or even itās transform:
objectReference = Instantiate(prefab, position);
and either make it a static reference, or pass it around some other way.
When you spawn it in code, add the spawned object to some sort of manager. Then have the enemies reference the manager and read out the transform/gameobject
this could work but how do i spawn them inside the manager
This is rapidly encroaching on āzero effort postingā territory here.
You need to start asking yourself āCan Iā¦?ā rather than asking the forum.
You will make significantly faster progress and you will actually learn.
Watch how this guy does it:
Imphenzia: How Did I Learn To Make Games:
Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly, two (2) simple steps to success:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. Thatās how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Donāt make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!
If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If thereās an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
Finally, when you have errors, donāt post here⦠just go fix your errors! Hereās how:
Remember: NOBODY here memorizes error codes. Thatās not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
- the description of the error itself (google this; you are NEVER the first one!)
- the file it occurred in (critical!)
- the line number and character position (the two numbers in parentheses)
- also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you donāt have to stop your progress and fiddle around with the forum.
Listen man i get that youre just trying to help and its not like iām a BEGGINER begginer, yeah im not thaaat good but i know haw to fix errors and implement simple features, its just that i spent a few hours on this and i cant seem to figure it out. This was my last resort
You could use a static event to notify enemies about a flare having been fired and also pass a reference to it.
public sealed class Flare : MonoBehaviour
{
public static event Action<Flare> Fired;
public static event Action<Flare> Died;
public bool IsBurning { get; private set; } = true;
private IEnumerator Start()
{
Fired?.Invoke(this);
yield return new WaitForSeconds(5f);
Die();
}
private void OnDisable()
{
if(IsBurning)
{
Die();
}
}
private void Die()
{
IsBurning = false;
Died?.Invoke(this);
}
}
Then enemies could subscribe to the event like this:
public sealed class Enemy : MonoBehaviour
{
private Flare flareToFollow;
private void OnEnable()
{
Flare.Fired += OnFlareFired;
Flare.Died += OnFlareDied;
}
private void OnDisable()
{
Flare.Fired -= OnFlareFired;
Flare.Died -= OnFlareDied;
}
private void OnFlareFired(Flare firedFlare)
{
flareToFollow = firedFlare;
}
private void OnFlareDied(Flare deadFlare)
{
if(flareToFollow == deadFlare)
{
flareToFollow = null;
}
}
}
I keep getting this error message, i realy donāt know what to do.
Assets\FlareBulletDestroyScript.cs(57,22): error CS1503: Argument 1: cannot convert from āFlareBulletDestroyScriptā to āUnityEngine.Flareā
yk what guys, thanks for the help but i guess im too dumb for this so im changing the whole concept. Im gonna have the player shoot a target that will trigger a light that the enemies follow
To be fair, code is a tricky beast. Since I got into the game late(old dogs new tricks, etcā¦) it takes me longer to wrap my head around certain concepts, so donāt expect to learn everything in one day. It just not a realistic expectationā¦
But as Kurt mentioned, it is best to do a bunch of simple tutorials(exactly as they do it, donāt modify it), and learn different code structures or ways to do things. Just to set a easy baseline you can pull from knowledge later on.
As diving head first into only wanting to do it your way, and learn at the same time, is going to cause nothing but frustration and headaches. Speaking from someone who did exactly that⦠lol.
Like take for example this snippet:
public void SetAdjacent()
{
foreach (var hex in Physics.OverlapSphere(worldPos, 2.0f))
{
if (coll != hex)
{
if (hex.TryGetComponent<Land>(out Land found)) { adjacentHex.Add(found); }
}
}
}
This same function, before yesterday, would have looked like a complete mess, and massive ābeating-around-the-bushā style of coding. But easily 10 years of just playing around with code on my free time, to finally learn to make something as beautiful and sleek like that.
And boy did I admire it, and probably stare at it for hours! lolā¦
But had I just followed tutorials, and learned this way, and even others, I would of had that as common knowledge a long time ago. And it would be just a super simple way to do it now.
Yeah, i mean i dove into the coding world about 3 weaks ago with ZERO knowlege, i had some baseline C++ knowlege from school but thats it. And i mean i was always intriuged by the idea of game developing and desided that what better way to start than now. And I did and the first week i didnt understand anything at all, all i did was copy code from tutorials and try to learn as mutch from them. On week 2 i wanted to start my own project with the knowlege i picked up and to my suprise it went good, i couldnāt get mutch of the code from my head but i sort of addapted the scripts i wrote from the tutorials, and finaly something started sticking, offcourse occasionally i would need tutorials for the stuff i couldnt underatand on my own, like scenes and stuff. In a week i had a finished 2D platformer and i was very proud. At week 3 i wanted to start an original 2D game and basically did it the same way i did the platformer (tried to figure out as mutch on my own and if i really couldnt i would search it up). My goal really is to master unity and coding because o really love doing this and would love to do it for a living. I applied for the brackeys game jam next week so hope that goes well. Btw im only 16 and its summer so im trying to use this free time to my advantage. If anyone here is also participating in the jam i would love to playtest their game and vice versa.
Well youāre definitely doing it right by starting young, wish I wouldāve dived into more back then. Retaining knowledge is much easier early on, as youāll find out too late in life, lolā¦
But it seems your problem is script to script communication. One good video that helped me, was from Jason Story, or his friend that helped do the videos:
If thatās the video I remember, he lists off a ton of different methods/structures that can be used for script communication. I personally went with a mix of Singletons, Static variables, and Inheritance(or possibly other methods Iām not sure the name of).
But there truly is a bunch of ways to do the same thing, which makes code very confusing, but also offers flexibility. So itās more of what makes sense to you, or what you found to be more performant than doing something the other way previously.
But I would(personally) advise against entering in any public displays just yet. As ācoders blockā is a real thing, and if everything blows up in your face, it could discourage you from ever trying again.
So hard life lesson, learn to master walking, before you try running.
Thanks, for the script to script comunication i usually used get component of a game object that i asigned publicly. I deffiniteley will give that video a watch.
As for the game jam, dont worry, this game jam (week long btw so plenty of time) wont disscourige me if it goes wrong, and im not going in expecting any awards or anything. I just wanna challenge myself, if i dont have i good idea for the theme i probably wont give it a try, and if i do ill make a simple game, sort of simmilar to the two i made in the past 2 weeks. If i dont finish it, thats allright. Most important part is that i learn something.