Jump to content
  • Advertisement

Zesi

Member
  • Content Count

    25
  • Joined

  • Last visited

Community Reputation

163 Neutral

About Zesi

  • Rank
    Member

Personal Information

  • Interests
    |designer|programmer|
  1. (2) Solved! Thank you. I have change my code for spawner only to.   private Spawner[] spawnerArray; void Start () { // other lines of code. spawnerArray = GameObject.FindObjectsOfType<Spawner> (); } void OnVictory (){ // other lines of code. foreach (Spawner spawner in spawnerArray) { Destroy (spawner.gameObject); } Now it destroy all spawner upon victory. However, there is an error, "The object of type 'Spawner' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." I have another script (Shooter) that is constantly accessing Spawner[]. What do I do to to that method once Spawner is all destroyed?  
  2. Hi Shaarigan. Thank you. 1) How do I set the audio file to not be null? I have alreadt attach the audio file to the audiosource via inspector. How do I script AudioSource to select the attached audio file instead of null? 2) Okay. Let me try the array method. Thank you very much!
  3. Hi all. Facing some slight problem again.   1) I am trying to play an AudioSource component on the current UI object.   2) I have 5 Spawner Prefabs Object in the scene. (Spawner is the script name attached to the GameObject "Spawner") private AudioSource victorySound; private Spawner spawner; void Start (){ victorySound = GetComponent<AudioSource>(); spawner = GameObject.FindObjectOfType<Spawner> (); } // In between there are other method calling for OnVictory void OnVictory (){ victorySound.Play (); Destroy ("Spawner") Invoke ("NextLevel", 3f); } void NextLevel(){ SceneManager.LoadScene ("VictoryScreen"); } It does load victory screen, meaning the invoke works. However, 1) it doesn't play the clip that I have attached to the inspector and gave me the warning that "victorySound is not assigned and it's default value will be null" 2) it doesn't destroy all the spawner objects at all.   What am I doing wrong here?   thank you all!   Edit: (2) Solved! Thank you. I have change my code for spawner only to. private Spawner[] spawnerArray; void Start () { // other lines of code. spawnerArray = GameObject.FindObjectsOfType<Spawner> (); } void OnVictory (){ // other lines of code. foreach (Spawner spawner in spawnerArray) { Destroy (spawner.gameObject); } Now it destroy all spawner upon victory. However, there is an error, "The object of type 'Spawner' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." I have another script (Shooter) that is constantly accessing Spawner[]. What do I do to to that method once Spawner is all destroyed?   I am still unable to play Audio.
  4. Zesi

    Scripting habits.

    Thank you so much for the detailed explanation Satharis and Alberth.   It really helps me understand more!   Thank you!
  5. Zesi

    Scripting habits.

    Ops! Spotted the typo!   Thank you very much Nypyren and Satharis! :)   Hi Satharis, could you explain further and show me an example what does it mean by "memory allocation/copying/loops that operate on large blocks of memory."?
  6. Hi. I can't advice you on game development as I am a beginner myself and I am still learning. I work as a teacher so the only advice I can give you is just start with a free version. A lot of kids have very short attention span and some may not pursue further. Every kid is different. Try out the free version first. Let him play around. Let him explore the software.   There's alos something I would like to add. There's many process in game development. There's the visual, creative side and the programming side. On the visual side, I would recommend to start messing around with blender. sculptris and gimp. Blender and sculptris does 3D and gimp is similar to photoshop. Both of which are free. If I am not wrong, Maya offer educational version for students for free. (someone might want to correct me if I am wrong) Check it out. Or even simpler, get him to draw, if he enjoys it!   As for the technical, programming side, I will leave it for others with better knowledge to advice you. I believe learning programming language at 13 is not a bad idea.   Good luck.
  7. Hi everyone. I would like to ask some advice with regards to scripting habit in C#.   I am currently reading Introduction to Game Design, Prototyping, and Development by Jeremy Gibson. Still practicing my scripting. I am trying to keep a good habit since I am still learning.   So anyway I seems to thought be able to find out through scripting, I find myself too "naggy" or "redundant" in my codes. Does it even make sense? lol. Anyway here is an example.   Both codes achieve the same result BUT, one of them is shorter. Vector2 FindCoordinates (){ Vector2 currentCoordinates = CalculateMouseClickToWorldUnit (); float currentX = currentCoordinates.x; float currentY = currentCoordinates.y; Vector2 roundOffCoordinates = new Vector2 (Mathf.RoundToInt(currentX), Mathf.RoundToInt(currentY)); return roundOffCoordinates; } 2nd one. Vector2 FindCoordinates (Vector2 rawWorldPos){ float newX = Mathf.RoundToInt(rawWorldPos.x); float newY = Mathf.RoundToInt(rawWorldPos.y); return new Vector2 (rawWorldPos.x, rawWorldPos.y);} My question is,   Does it matter? Does long lines of codes decrease performance of a game?   I will be pursuing to build a 3D game after I build up my skill but what are the other ways to maintain a good habit in scripting games?   Thank you all.
  8.   thank you so much Satharis! I really appreciate your help!   Finally got to figure this out! :)
  9. Yup! I use debug.log and debug.logerror to check if I am right. Most of the time I am confident when it's straight forward.   This is one of those times when I have no confident whether I understand or not.   I keep doubting myself: "Do I fully understand the process or am I just copying whatever that was written"   :(
  10. It isn't a float, it's an int, as the name implies. If you look at the Unity documentation it gives this signature: SetInt(string key, int value); Meaning it takes an int. If you give it a float then it will implicitly convert it into an int, so basically it will shave the decimal part off.   What happens is it grabs the int based on the key you give it. If we look at the documentation:   public static int GetInt(string key, int defaultValue = 0); You can see that it returns an int, and if it can't find the key you gave it then it will return defaultValue. If you didn't provide a defaultValue then it will return 0. It doesn't return true or false, it just returns a number.   Ohhh! Sorry my mistake. Got it about the float and int. I messed it up.Thank you!   I can see the whole picture now.   I understand that it returns either the key I have set, in this case "1". and if there is no key, it's default will be 0. No, it doesn't return true or false.   However, it goes through this process in the IsLevelUnlocked () method. 1) GetInt gets the int that I have assigned in the preferences (from the int I have SetIn) 2) I have assigned this int into another name, levelValue 3) so now it goes true a boolean operations; bool isLevelUnlocked = (levelValue==1) this means that if my GetInt was "1", the bool isLevelUnlocked is true.   4) Goes through the conditional, and as a result it will return either a true or a false.   Am I getting the process right so far?   thank you very much!
  11. It doesn't "return" anything, it just calls the function with what you pass it. In this case the function wants a key and a value for that key, key being a string. in the file it might be KEY1732=1 if your key was "KEY1732." The details don't matter though. The 1 you're setting is the value the key is set to.   You have the idea right, yes. GetInt just literally grabs the number that is written in the file and interprets it as an integer, and then returns that integer to you. Then you're setting int levelValue to it. Everything in memory is just a number. The numbers: 10, 37, 4123 can all be interpreted as a boolean just by changing their type to bool. Usually any non-zero number is interpreted as true and 0 is interpreted as false.   Hi. Thank you for your reply!   Got the first part!.   So am I right to say that if I were to set, PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); since that "1" is just a float, I can set any number I want it to be. For example. 999.   So what happens during this int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ()); Since I set the key to 999,   GetInt will give me 999.   If i dont satisfy the conditions, it will return false.   Am I getting it right so far? Sorry if I got it wrong again. :(   thank you very much.
  12. Never programmed any Unity, but yeah, the "int" looks like the value associated with the key, I'd expect something like int answer = 42; // obtain the value you want to store (normally from your game data instead of a hard-coded number). PlayerPrefs.SetInt("answer_to_life_universe_everything", answer); // Stores value 42 with key 'answer_to_life_universe_everything' My guess is, "level is unlocked" is either true or false, ie it's a boolean notion. For reasons I don't know, they decided to use the integer value 1 for "true", and presumably 0 for "false". Other integer values are not used, since the other values have no translation to/from the boolean notion "level is unlocked". It is not a limitation in PlayerPrefs.GetInt, it's a limitation in the range of values that you can usefully store for this particular player preference.       The above code does 2 things: - "int levelValue = .." retrieves the 'unlocked level' number you inserted with the 'SetInt call, ie the 1 you inserted with the 'SetInt' above - "bool isLevelUnlocked = .." converts that integer number back to a "level is unlocked" boolean (ie from 0/1 to false/true).   To just get the number you inserted, don't do the second thing, ie skip the conversion to a boolean, ie something like int answer = PlayerPrefs.GetInt("answer_to_life_universe_everything"); // Do something with the answer In general, you do want to check that the returned value from the preferences is sane, people may have edited the storage in some way, and then you may get weird numbers like -83 or 123456789  (very useful if that number is the number of lifes that I have in your game :D )   Hi. Thank you so much for explaining. I *think* I may have got it after reading what you write but I am still not confident enough. Indeed, what I am trying to do is a boolean operation. However, "PlayerPrefs" can only set int float and string. Meaning a boolean operation is not allowed, so there is a need to do that code.   Do correct me if I am wrong.   the operation PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); returns 2 things: 1) the level_key + the int value to string.  (i.e level_key_01 as a string) that the player has set 2) returns 1 IF it satisfy my "if" statement.   As a result, when I perform a int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ()); What I will GET is the int value of 1, since my previous operation returns 1.   And then the next if statement states that if my conditions are satisfied, it will return true, otherwise return false, making the whole IsLevelUnlocked() method a bool operations.   Am I getting it right so far?   Sorry, I have never coded before and I want to make sure I understand every line and not just copy and paste.   Thank you!!
  13. hi all. I am going through some tutorial with regards to PlayerPrefs.   there is 1 part of the script I do not seems to be able to understand.   Hope someone can help me. Thank you!   Okay, correct me if I am wrong about PlayerPrefs. Here's what I understand from the unity documentations   1) Only able to set int, float or string. PlayerPrefs give players access to set preferences.   2) Specifically I am asking about PlayerPrefs.SetInt and GetInt.   From Unity Documentations: public static void SetInt(string key, int value); I understand the key part. the key part is the string I have set to. 3) What does the "int value" represent? Anything I have set it to right? Okay here's what I do not get. public static void UnlockedLevels (int levels) { if (levels <= SceneManager.sceneCountInBuildSettings - 1) { PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true } else { Debug.LogError ("invalid scene outside of build order"); } } public static bool IsLevelUnlocked (int levels) { int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ()); bool isLevelUnlocked = (levelValue == 1); if (levels <= SceneManager.sceneCountInBuildSettings - 1) { return isLevelUnlocked; } else { Debug.LogError ("Trying to query level outside of build order"); return false; } } First part of the script, unlockedlevel, PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true Now, what I do not get is, the int value assigned is "1", where 1 being true. Okay. 2nd part of the script, bool. int levelValue = PlayerPrefs.GetInt (Level_Key + levels.ToString ()); bool isLevelUnlocked = (levelValue == 1); How does the Getint allows me to return 1 or 2,?   The instructor in the video mention that the code "PlayerPrefs.SetInt (Level_Key + levels.ToString(), 1); //use 1 for true"   but I want to know why. I am trying to wrap my head around this for a long time. there must be something I am missing!   thank you everyone!
  14. It's the same fundamental problem as a 2d game, you want to prevent something from moving out of bounds. The only two logical ways to do that are to move them out of bounds and then snap them back in, or determine how far you are going to move and then only move them by that amount.   The problem with using distance is that distance is magnitude. If your "right game bound" is right of your character, positive on the x axis, then as you approach it the vector will shrink until you move past it. Then it will be a vector pointing from your character to the bound still, except the bound is in the opposite direction, so the distance will start growing again. He suggested you compare the x coordinates of the vectors instead. if(vec1.x > rightVec.x) kind of thing.   You could either pass a delta time between frames or have some code that takes delta times and applies it to a "running time" variable that you can compare against. Once enough time has elapsed you run the code. Just don't use something like system time since that usually can be changed.   Thank you so much for the reply.   Understood. I manage to use Mathf.Clamp to do the job.   Actually, I have no codes related to time on my scripts. I probably will have to work on that. The only "Time" that I use is Time.deltaTime.
  15. Omg. I am so dumb. :(   I knew I did something wrong.   The default setting for RigidBody2D was Dynamic.   All I had to do was change it to Kinematic and the problem goes away!     I will give it a try later.   Thank you everyone for the help.   My pure carelessness. :(
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!