Search Unity

Space Shooter Tutorial Q&A [ARCHIVED]

Discussion in 'Community Learning & Teaching' started by Valdier, Jan 12, 2014.

Thread Status:
Not open for further replies.
  1. Deleted User

    Deleted User

    Guest

    Hi, I've been trying to figure out what the problem is. but since I'm new to both unity and coding, I really have no idea.... I have an error message as "NullReferenceException: Object reference not set to an instance of an object
    DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContacts.cs41).
    I have explosion animations and sounds but they just don't disappear when colliding each other. help please!
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    can you copy and paste in the OnTriggerEnter function code please, so we can have a look.

    just as a quick question before you do that, have you added the Boundary tag, and assigned the Boundary tag to the boundary gameobject?
     
  3. Deleted User

    Deleted User

    Guest

    uh huh yep cheked it! I'm like person who usually don't miss that kind of thing.
    this is my third time of doing this same tutorial and I've got the first one right, but second one and third one wasn't lucky.

    here's code. (I've tried to solve the problem all by myself for like five hours but wasn't quite worth it, really haha)
     
    Last edited by a moderator: Dec 17, 2014
  4. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5.  
    6. {
    7.     public GameObject explosion;
    8.     public GameObject playerExplosion;
    9.     public int scoreValue;
    10.     private GameController gameController;
    11.  
    12.     void Start ()
    13.     {
    14.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    15.  
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent <GameController>();
    19.         }
    20.         if (gameController = null) {
    21.      
    22.             Debug.Log ("Cannot find 'GameController' script");
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.  
    29.                 if (other.tag == "Boundary")
    30.                 {
    31.                         return;      
    32.                 }
    33.  
    34.                 Instantiate (explosion, transform.position, transform.rotation);
    35.  
    36.                 if (other.tag == "Player")
    37.                 {
    38.                 Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    39.                 }
    40.  
    41.                  gameController.AddScore (scoreValue);
    42.                 Destroy (other.gameObject);
    43.                 Destroy (gameObject);
    44.     }
    45. }
    46.  
    47.  
    48.  
    49.  
     
  5. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    cheers Eric that was quick :)

    just looking at line 20 above where you have
    Code (CSharp):
    1. if (gameController = null) {
    you will need to change this to
    Code (CSharp):
    1. if (gameController == null) {
    as a single = is an assignment operator, and == is the comparison operator.

    this is assigning the gamecontroller to null as opposed to checking it.
    and in line 41 in your code is looking for the game controller to add a score and it doesnt exist and throws an error.

    see how that works Eric.
     
    Deleted User likes this.
  6. Deleted User

    Deleted User

    Guest

    it works perfectly. thanks a lot! I can't believe I've missed that though. haha
     
    OboShape likes this.
  7. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    no worries, glad to lend a hand
     
  8. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Ok, so I got a problem that im trying to figure out being a newbie and all. If I write a script to make the camera shake when space ship is hit- How can I access the method called Shake() from a script called ShakerScript and call it from my EnemyScript so once my player gets hit after the Attack() method is called in the latter script- the screen will shake a bit... how do I do that? Again, I dont need help with the Shake() method in the ShakerScript, I just need help to call it in the EnemyScript.

    Thanks again.
     
  9. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Ok so I have a delima... I have done a couple of scripts working that whenever the player and enemy object collide, they both take damage. Let's say for example the player ship has 10 life points. The enemy has 2 life points. If the run into each other they both lose a point and if they come in contact for a second time the enemy blows up and the player ship is left with 8 life points. The problem is if I put more that one enemy object on the scene from my prefabs to test...the last one that was added works as intended, the other added before show as if the have 2 life points on the inspector window but they never take damage and blow up. Can anyone help me with my delima? Thanks again.
     
  10. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    hi there, thought id ask and see if i can help a wee bit :)

    ok, just to clarify based on what your saying, each enemy has 2 hit points, player has 10.
    your dragging enemy prefabs into editor, and then testing with say 3 enemies? or are they getting instantiated via code?

    last enemy prefab that you 'create' in works fine, and others do nothing.
    Does the problems lay in damaging the player as well, or is it purely the fact that the 'enemy' doesnt take damage and destroy?

    can you post the script that deals with the enemy taking damage please as well. if its a problem localised to the enemy receiving damage (or PM me it across if its mahoosive)
    it does sound from first thoughts that your possibly using a findObjectWithTag (singular) but dont really want to speculate.
     
  11. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Yes now that I recall I am using findObjectWithTag. Yes my ship will take damage from all prefabs but only the last enemy will explode while the other ones continue to do damage but their energy wont deplete on the inspector. I cannot post script now because I am away from my pc for the next 7 hours...freakn day job :p.
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    no worries i know work getting in the way sucks lol, ill get a look later in the morning here if you like, (8pm here UK at present).

    if its the FindWithTag() this only returns the first active one it finds with that specific tag, hence where the issue may lie.
    http://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

    might be better to utilize the colliders/trigger on each object, say the enemy and the player.
    fundamentally the same as you have done in the destroybycontact script by comparing the objects collider and what collider made contact. something to think about :)
     
  13. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    I still cant get the object I drop on the scene to test to get destroyed- just the last one add.. here is the sample script
    Code (CSharp):
    1. GameObject enemy;                        
    2. EnemyHealth enemyHealth;
    3. bool enemyInRange;
    4.  
    5. void Awake()
    6.     {
    7.         enemy = GameObject.FindGameObjectWithTag("Enemy");
    8.         enemyHealth = enemy.GetComponent<EnemyHealth>();      
    9.     }
    10.  
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.gameObject == enemy)
    15.         {
    16.             enemyInRange = true;
    17.         }
    18.     }
    19.  
    20.  
    21.     void OnTriggerExit(Collider other)
    22.     {
    23.         if (other.gameObject == enemy)
    24.         {
    25.             enemyInRange = false;
    26.         }
    27.     }
    28.  
    29.  
    30.     void Update()
    31.     {
    32.         if (enemyInRange)
    33.         {
    34.             EnemyAttack();  
    35.         }
    36.         if (enemyHealth.enemyCurrentHealth <= 0)
    37.         {
    38.             Destroy(enemy.gameObject);
    39.         }
    40.     }
    Hopefully I pasted this right... Any help would be appreciated- Thanks again :)
     
  14. Azbark

    Azbark

    Joined:
    Dec 24, 2014
    Posts:
    1
    HI I was using your tutorial and it is working great but I have one problem and that problem is that when I shoot the asteroid it shows it getting blown up but it dose not disapeer,Help would be greatly appreciated. (I am using the done models and scripts)
     
  15. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi Azbark, and a Merry Xmas to you :)

    couple of questions, just to get a feel for what is happening if you dont mind.

    Your using the Done_DestroyByContact script from the Done folder, have you edited it at all?
    so the asteroids fall and tumble as they should?
    then when you fire the laserbolt and it makes contact with the asteroid... the laserbolt dissapears and you see the explosion?, but the asteroid doesnt get destroyed?

    Are you getting any errors showing?
    (look at the bottom of the screen it shows the last error, or in the console window)
     
    Last edited: Dec 27, 2014
  16. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    you get your code to work @Beta4 and find a solution?
    i know were were continuing it Via PM Conversation as it was more of a progression as opposed to the shooter tute itself.
     
  17. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Yes thank you, Happy New Year here from the States. I thought I had replied that I got it to work. thanks again Obo!
     
    OboShape likes this.
  18. ConnorTheScot

    ConnorTheScot

    Joined:
    Jan 4, 2015
    Posts:
    7
    Good afternoon all,
    I am at Part 09 - Creating Hazards on this tutorial, and thus far it has been excellent.
    However I have stumbled upon a bit of a problem with the DestroyByContact script.
    The problem I have is then after implementing the portion of the code to ensure the asteroid will not be destroyed by the Boundary, everytime the game is played the asteroid is destroyed very quickly after play mode is entered.
    The Boundary gameObject is tagged with the Boundary tag and the code below refers to this tag.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5. {
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         if (other.tag == "Boundary")
    9.         {
    10.             return;
    11.         }
    12.         Destroy(other.gameObject);
    13.         Destroy(gameObject);
    14.     }
    15. }
    Any reason why this might be happening? I can provide further code if required, however I need to pop away from my desk for a while.

    Any help is appreciated.
     
  19. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Alright Connor,

    ok so the tag in your destroy by contact script, and the tag in the tag manager assigned to the boundary are all the same case and spelling?

    any errors showing in the console window at all?

    (if you cant see console window go to Window > Console in the main menu bar.

    theres one thing you could check to see what its colliding with to give you an idea where to start looking, try this debug.log line in the destroy by contact script and it will tell you what its hit (well its name in the scene)

    Code (CSharp):
    1.     void OnTriggerEnter (Collider other)
    2.     {
    3.         Debug.Log("Collided with  : " + other.name); // just to test, should display in console window

    if you want to look at whats happening on a frame by frame level as your game starts.
    the control buttons at the top of the editor window, press the PAUSE button, then press PLAY, and then you can use the button on the right to STEP through each frame.

    give a shout later let us know if anything comes to light :)

    Cheers
    ObO
     
    Last edited: Jan 4, 2015
  20. ConnorTheScot

    ConnorTheScot

    Joined:
    Jan 4, 2015
    Posts:
    7
    Hi Obo,
    Thank you for the quick response earlier. That is me home now and I can now try and find some resolve to this :)
    Below is snippets from both the DestroyByContact script and the Boundary gameObjects inspector.



    No errors have been given in the console window.

    This debug script has advised me that the asteroid collided with the bolt gameObject and destroyed it. Since discovering this I played the scene frame by frame and have noticed that when the scene starts a bolt is spawned and does collide with the asteroid, thus destroying it.
    I am going to dig to see if I can stop this from happening, but any assistance is appreciated :) Thank you greatly Obo!
     
  21. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    cool, there shouldnt be a bolt on the screen at startup.

    before you press play, have a look in the heirarchy in your editor to see if there is a 'Bolt' gameobject.
    if there is, remove it as all the bolts are created by the instantiate method in the playercontroller script.

    if thats not it.

    press PAUSE, and then PLAY so you are looking at the first frame of the game.
    now have a look in the heirarchy, if there is a Bolt(Clone) then this indicates an instantiated object so somethings created it from within your playercontrollerscript before you are ready for it, and also if this is it, then its not instantiating at the ShotSpawn gamobject location.

    should narrow the problem down a bit :)
     
  22. ConnorTheScot

    ConnorTheScot

    Joined:
    Jan 4, 2015
    Posts:
    7
    Hi OboShape,

    Right on the money.

    So what I had wrong was in the hierarchy there was a instance of 'Bolt' within the Shot Spawn gameObject, and there was an instance of the 'bolt' gameObject being spawned from the Bolt gameObject.

    Deleting both of these from the hierarchy sorts the problem.

    Quick question for you Obo, as there is no longer the Bolt gameObject which contains the capsule collider and then has the VFX quad as a child will this cause any problems. I know that after deleting all these objects I can still fire the shots, however in my head not having these in the hierarchy just seems strange, is the hierarchy only related to the scene when playing and not to the project and its assets?

    That may have not made any sense, and please do just say if it doesn't :)

    Thanks!
     
  23. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    cool, great its working!

    The Debug, console and pause and step controls can be a handy weapon in the fight against nasty bugs :)

    basically yes :), your heirarchy is whats currently within your game scene.
    but what you have done with the Bolt, is to make one within your scene so it looks the way you want it. (like in the creating shots vid)
    then you have dragged this into the Prefab folder, so you have, by doing this, created a template or blueprint of your Bolt (and you will notice that your gameobject in the heirarchy is blue too indicating a prefab).
    You have deleted the one in the scene, but your blueprint is still stashed away for use later, ie the one you dragged into the bolt slot in the player controller script on the players ship.

    what your instantiate call does in the player controller firing code, is to instantiate/create a clone of this blueprint and put it in your scene. (basically, but for more deeper info look in tutorials section for classes)

    have a look through the tutorials page at the following which deals with prefabs and their theory
    http://unity3d.com/learn/tutorials/modules/beginner/editor/prefabs-concept-usage

    and also the learn session archive which is a brill collection of info in realtime sessions :)
     
    Last edited: Jan 4, 2015
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Obo: Thanks for the fast answers!

    When we use "Instantiate" the function takes a "object" and makes a clone/copy of it, and adds it to the current scene.

    It is possible to instantiate a copy of an object in the scene, so you could have a "bolt" object in the scene (preferably turned off / deactivated) and you could have a reference to that and instantiate it - but that leaves an unwanted object in the scene...

    So - with prefabs, you can have that template or blueprint (as Oboshape mentions) saved as an asset in your project, and instantiate those, so your scene stays clean and you only instantiate objects you need when you need them.
     
    OboShape likes this.
  25. bobarama

    bobarama

    Joined:
    Jan 1, 2015
    Posts:
    1
    Just worked through the Space Shooter Project, all but "Building the Game." I have already worked through the live presentation #44 as mobile is my target. The Space Shooter tutorial ends before the Enemy Ship is added and also BG Scroller. Are there tutorials that address these elements.

    Thanks, Bob

    I'm just an old dog learning new tricks.
     
  26. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning Bob :)

    If memory serves, within the Done folder of the Shooter project you downloaded from Asset Store it has these parts included.
    I think Adam done this as something for us students to look into extending what we had learned at our own pace.

    but if you look in the Done > Done_Scenes folder you will find Done_Main,and open up that scene in your editor you can see the completed project, then pick it apart from there if your comfortable doing that.

    for the scrolling background, if you look at Background object in the heirarchy, there is a 'Done_BGScroller' script attached that deals with the scrolling of the background image.

    for the enemy, there is a prefab in the Done_Prefabs folder for Done_Enemy_Ship, theres a fair bit in it, but if you take it step by step and look through it. theres the same mover script as you have done already as well as an additional Evasive Manouvre and Weapon Controller script.

    for the enemy, if you look at the Done_GameController you will see an additional hazard in the array for the enemy ship, this creates an enemy in the scene and then its own scripts take care of it (scripts mentioned above)

    maybe Adam has plans for a future date, but certainly any questions.. ask away :)
     
  27. RipperRoo

    RipperRoo

    Joined:
    Aug 27, 2014
    Posts:
    15
    Hi,

    i had two problems with stretch goals, and i want to share the results and ask maybe for a better solution on the 2nd problem.

    the first problem was the inertia in the controls, that was easily fixed by using getinputRAW instead of getinput.

    2nd problem is that your ship could fly a bit out of the boundary and when u stop flying in the direction it flies a bit back. this looks especially bad when using rawinput, because ur ship will just snap back in. u can see it here: https://dl.dropboxusercontent.com/u/24667461/Project RKK/Builds/v0.21/v0.21.html

    after a bit of research it seems that it has to do something with fixedupdate and the physics, i want to quote zerot from a reddit post here:
    as he and a couple of other persons from the unity3d irc and unity-insider.de forum suggested i used "invisible walls" around the map so now u cant leav it anymore.

    But i still wanted to know if there was a better way inside the code since mathf.clamp didnt bring the expected result because of the render/physics/update order.


    PS: thank you very much for this tutorial! it was great fun, and my game is growing a bit:
    https://dl.dropboxusercontent.com/u/24667461/Project RKK/Builds/v0.22/v0.22.html

    thanks to you adam and the unity team
     
  28. SetiFX

    SetiFX

    Joined:
    Jan 9, 2015
    Posts:
    2
    I have completed the tutorial and uploaded it to the web. It works! However, the tutorial ends prior to a full build beyond scene 1. I would like to continue the project by adding a couple more scenes:

    1. Add the additional asteroids that are included in the prefab,
    2. Add an alien spacecraft that maneuvers and shoots

    I would also like to randomly vary the size, rotation and speed of the asteroids, and number of aliens.
    I am a 100% noob. This is the first game I have ever built and I am not a programmer. I do have a background in animation and motion graphics and worked with scripts so I was able to complete the exercise, including the use of the new Text UI, but I am a novice and do not know where to go.

    Any suggestions would be greatly appreciated.
     
  29. RipperRoo

    RipperRoo

    Joined:
    Aug 27, 2014
    Posts:
    15
    SetiFX everything u ask for is done in the Done folder as kind of stretch goals...

    just compare what u ve done to what is done in the finished project, im 100% noob as well
    and i managed to add the variation of astroids, random rotation and speed + the spacecraft maneuvering aliens.

    Maybe just open 2 instances of unity and compare step by step what u have and what is different in the finished project, try to understand it, and then u can easily copy and extend it. If u encounter any problems with that, i could maybe help u, but beware im really bad at programming :)
     
    SetiFX likes this.
  30. SetiFX

    SetiFX

    Joined:
    Jan 9, 2015
    Posts:
    2
    Ok, I get it now. I see the done folder and everything in it including the scripts etc. This should be ez.....
     
    RipperRoo likes this.
  31. Ridhwan Ridhu

    Ridhwan Ridhu

    Joined:
    Jan 10, 2015
    Posts:
    2
    I've got some problem.There are no textures on my player space ship.Here are the images.Plzzz help
     

    Attached Files:

  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    As Oboshape has responded (THANKS OBOSHAPE!), the enemy ships and other elements are "stretch goals" the the student can try to achieve. The finished product can be found in the "Done" folder as an example solution. We may do these additional steps as a tutorial or live session, but currently, it's not on the books.
     
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This was intentional. The use of Input.GetAxis (rather than Input.GetAxisRaw) was to give a feeling that the ship was moving or easing into the banked position. If you don't like that, and you want the ship to snap, you need to use Input.GetAxisRaw.


    didn't mind that easing back into the game board, but if it really annoys you, I'm sure you can try to find a solution. This is due to physics...

    You could try executing the clamp in "LateUpdate()" and see if that solves the problem. If I get the chance, I'll look into a solution, but it won't be immediate.


     
    RipperRoo likes this.
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    As Oboshape has responded (THANKS OBOSHAPE!), the enemy ships and other elements are "stretch goals" the the student can try to achieve. The finished product can be found in the "Done" folder as an example solution.

    We hope that people do carry on and expand this example project and add things like additional ship types and asteroid; different difficulties (3 shots to destroy a big asteroid for bigger point values?); additional levels; etc.

    Many people have, so have a go!

    Some have even made it up onto the App Store and Google Play as finished games.

    PS: Look at the archived live training session on "Space Shooter for Mobile" as an example of upgrading this project to the the new UI system.
     
  35. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You are in "wireframe" mode. Note the pulldown under the "Scene" tab:


    You can change this setting to several other options.

    Please see the manual for more information (follow these URLs as a path from the root of the manual to the specific setting you are looking for - Draw Mode: Wireframe:
    http://docs.unity3d.com/Manual/
    http://docs.unity3d.com/Manual/UnityBasics.html
    http://docs.unity3d.com/Manual/LearningtheInterface.html
    http://docs.unity3d.com/Manual/SceneView.html
    http://docs.unity3d.com/Manual/ViewModes.html
     
  36. Ridhwan Ridhu

    Ridhwan Ridhu

    Joined:
    Jan 10, 2015
    Posts:
    2
    Thank You very much.It helped.I am developing Survival Shooter.So I wouldn't have been able to complete without your help.Thank You very much
     
  37. tylergibbs

    tylergibbs

    Joined:
    Jan 14, 2015
    Posts:
    1
    Hello, I'm new to Unity and I was wondering what Scroll Speed and Tile Size Z I have to put for the Scrolling BG script. I already have it as a component to 'Background', I just need to know the Tile Size Z and the Scroll Speed.
     
  38. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What's best in this case, is to load the done scene from the project and check the done BG object.

    Now, that being said, things like speed are all up to you. You are in charge! You can pick any value you feel is correct.
     
  39. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    I would add a power ups invicible mode and a Lock unlock system.My idea si to creare 60 different level and i ma ary the beginning with c Sharp
     
  40. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
    I have been watching the mobile conversion tutorial video however
    I encountered a weird bug.

    Every time I touch a different area of the iphone screen (while using unity remote 4)
    the ship draws a vector to that position (even though it is not in the movement zone UI)
    and moves towards that position.

    I've added the "protection" codes to distinguish pointedId's and to check
    the "touched" boolean however it just doesn't even go in that code.
    It's as if the entire canvas is touchable and it gets it's vector from an unknown source.

    I debugged it multiple times with Debug.Log and haven't found where
    does it come from.

    I know this might be weird to fix, since you can't see my code.
    I copied the exact code provided in the tutorial, how can this be?
    (forgive me, this is my first post since its really frustrating).

    ~again thanks.
    ~Andrei.
     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Andrei: It is possible it's a bug in my code, but I thought it was thoroughly tested... Have you used the code attached to the archive page? Have you made any changes? Don't forget you can post code here if you use code tags, or you can post to a paste site (pastebin, etc) and post the url here.
     
  42. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
  43. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
    How do I use code tags? :)
     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  45. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
    This is the PlayerController:
    Code (CSharp):
    1. public class Boundary
    2. {
    3.     public float xMin, xMax, zMin, zMax;
    4. }
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public float tilt;
    10.     public Boundary boundary;
    11.    
    12.     public GameObject shot;
    13.     public Transform shotSpawn;
    14.     public float fireRate;
    15.     public SimpleTouchPad touchPad;
    16.     public SimpleTouchAreaButton areaButton;
    17.    
    18.     private float nextFire;
    19.     private Quaternion calibrationQuaternion;
    20.    
    21.     void Start () {
    22.         CalibrateAccelerometer ();
    23.     }
    24.    
    25.     void Update ()
    26.     {
    27.         if (areaButton.CanFire () && Time.time > nextFire)
    28.         {
    29.             nextFire = Time.time + fireRate;
    30.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    31.             audio.Play ();
    32.         }
    33.     }
    34.    
    35.     //Used to calibrate the Iput.acceleration input
    36.     void CalibrateAccelerometer () {
    37.         Vector3 accelerationSnapshot = Input.acceleration;
    38.         Quaternion rotateQuaternion = Quaternion.FromToRotation (new Vector3 (0.0f, 0.0f, -1.0f), accelerationSnapshot);
    39.         calibrationQuaternion = Quaternion.Inverse (rotateQuaternion);
    40.     }
    41.    
    42.     //Get the 'calibrated' value from the Input
    43.     Vector3 FixAcceleration (Vector3 acceleration) {
    44.         Vector3 fixedAcceleration = calibrationQuaternion * acceleration;
    45.         return fixedAcceleration;
    46.     }
    47.    
    48.     void FixedUpdate ()
    49.     {
    50.         //PC Controls
    51.         //      float moveHorizontal = Input.GetAxis ("Horizontal");
    52.         //      float moveVertical = Input.GetAxis ("Vertical");
    53.        
    54.         //      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    55.        
    56.         //      Vector3 accelerationRaw = Input.acceleration;
    57.         //      Vector3 acceleration = FixAcceleration (accelerationRaw);
    58.         //      Vector3 movement = new Vector3 (acceleration.x, 0.0f, acceleration.y);
    59.        
    60.         Vector2 direction = touchPad.GetDirection ();
    61.         Vector3 movement = new Vector3 (direction.x, 0.0f, direction.y);
    62.         rigidbody.velocity = movement * speed;
    63.        
    64.         rigidbody.position = new Vector3
    65.             (
    66.                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    67.                 0.0f,
    68.                 Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    69.                 );
    70.        
    71.         rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    72.     }
    73. }
    SimpleTouchPad:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
    7.    
    8.     public float smoothing;
    9.    
    10.     private Vector2 origin;
    11.     private Vector2 direction;
    12.     private Vector2 smoothDirection;
    13.     private bool touched;
    14.     private int pointerID;
    15.    
    16.     void Awake () {
    17.         direction = Vector2.zero;
    18.         touched = false;
    19.     }
    20.    
    21.     public void OnPointerDown (PointerEventData data) {
    22.         if (!touched) {
    23.             touched = true;
    24.             pointerID = data.pointerId;
    25.             origin = data.position;
    26.         }
    27.     }
    28.    
    29.     public void OnDrag (PointerEventData data) {
    30.         if (data.pointerId == pointerID) {
    31.             Vector2 currentPosition = data.position;
    32.             Vector2 directionRaw = currentPosition - origin;
    33.             direction = directionRaw.normalized;
    34.             Debug.Log (direction);
    35.         }
    36.     }
    37.    
    38.     public void OnPointerUp (PointerEventData data) {
    39.         if (data.pointerId == pointerID) {
    40.             direction = Vector3.zero;
    41.             touched = false;
    42.         }
    43.     }
    44.    
    45.     public Vector2 GetDirection () {
    46.         smoothDirection = Vector2.MoveTowards (smoothDirection, direction, smoothing);
    47.         return smoothDirection;
    48.     }
    49. }
    SimpleTouchAreaButton:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class SimpleTouchAreaButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    7.    
    8.     private bool touched;
    9.     private int pointerID;
    10.     private bool canFire;
    11.    
    12.     void Awake () {
    13.         touched = false;
    14.     }
    15.    
    16.     public void OnPointerDown (PointerEventData data) {
    17.         if (!touched) {
    18.             touched = true;
    19.             pointerID = data.pointerId;
    20.             canFire = true;
    21.         }
    22.     }
    23.    
    24.     public void OnPointerUp (PointerEventData data) {
    25.         if (data.pointerId == pointerID) {
    26.             canFire = false;
    27.             touched = false;
    28.         }
    29.     }
    30.    
    31.     public bool CanFire () {
    32.         return canFire;
    33.     }
    34.    
    35. }
     
  46. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
    How can I send you the prefab information adam?

    again thank you so much for helping me.
     
  47. Telsek

    Telsek

    Joined:
    Dec 3, 2014
    Posts:
    9
    I think it might be connected to the frame rate...
    it's running quite sluggish at my iphone. I tried 2 devices and it is the same on both.
    How do i reduce the quality?
     
  48. thefallingboy

    thefallingboy

    Joined:
    Dec 17, 2014
    Posts:
    13
    hi, this is my first post and i want to ask some questions if someone can help me, D:
    I was on the thirteenth part of the tutorial, when I got the bright idea to rename one of the scripts (bad idea), since then, I am fighting with an alert that appears in yellow, it says "The referenced script on this Behaviour is missing!" seeking by forums and communities I found many solutions, delete the meta files (which are hidden), review the names of the files on the physical folders are the same as they have in Unity, and so far nothing has worked, alerts are still there.

    i add two images from the project, one showing the code in the scripts and the yellow alert and another showing the (***) yellow alert with the asteroid and features on inspector.

    if some know how to fix this, or i just need to ignore them (im gonna be really happy ignoring them xD) or i just need to start the entire tutorial again xD

    sorry for the bad english google traductor FTW
     

    Attached Files:

  49. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning Felix, welcome to the forum :)

    its just looking for a script/object/reference that isn't there any more, its likely after you renamed something, you havent updated the scripts that are looking for the original component/object.

    dont ignore warnings or errors they're there to assist you :)

    what script did you rename?
    it started when you renamed a script?
    did you rename the script back to see if error would clear?

    as a note, when you rename a script you have to also change the class name within that script (the bit at the top) to be the same.
    also any references to the old script are broken (as it cant find the old name), like the ones you drag into the public slot on the inspector, so these have to be dragged in again.
     
  50. thefallingboy

    thefallingboy

    Joined:
    Dec 17, 2014
    Posts:
    13
    well at this point i made a lot of changes trying to solve this, i go back some steps on the tutorial deleting some components like some scripts and prefabs i made through the steps, thinking maybe this solve the problem.

    i was on the twelve part of the tutorial when i made the change i rename the script, "destroyByContact" to "DestroyByContact" just because the other scripts were "DestroyByBoundary" "DestroyByTime" etc etc, and then this started to happen.

    i go back to eight part of the tutorial and go part by part but when i going to instantiate the explosion of the asteroid the alert start, and something more, i just realized, when i click in some alert on the Hierarchy the object "explosion_asteroid(Clone)" start blinking like if u press F on the Hierarchy (like the image at the end).

    And now i remember i dont change the name of the class when i change the name of the script. But at this point i already delete that and others scripts and this still happens D:

    i try to solving using the script on the Done folder just comment (with this -> /* */) the part of the code about advanced gamecontroller
     

    Attached Files:

Thread Status:
Not open for further replies.