Search Unity

Variable rend of unit has not been assigned

Discussion in 'Scripting' started by Wolfgabe, Sep 27, 2016.

  1. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay I was kinda starting over with my RTS project and looking at some tutorials on unit selection everything seems to work fine with no errors but when I go to test the units will not change color when selected and I keep getting this error that says the variable rend of Unit has not been assigned

    UnassignedReferenceException: The variable rend of Unit has not been assigned.
    You probably need to assign the rend variable of the Unit script in the inspector.
    Unit.Update () (at Assets/Scripts/Unit.cs:21)

    I checked my code and I cannot seem to find any errors. I am not sure what I am doing wrong

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Unit : MonoBehaviour
    5. {
    6.     public bool selected = false;
    7.     public Renderer rend;
    8.  
    9.     private void Update()
    10.     {
    11.         if (GetComponent<Renderer>().isVisible && Input.GetMouseButtonUp (0))
    12.         {
    13.             Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
    14.             camPos.y = CameraOperator.InvertMouseY(camPos.y);
    15.             selected = CameraOperator.selection.Contains(camPos);
    16.         }
    17.  
    18.         if (selected)
    19.             rend.material.color = Color.red;
    20.         else
    21.             rend.material.color = Color.white;
    22.     }
    23. }
    24.  
    edit Tried adding private float Material but still getting the error


    EDIT Okay never mind now its working properly. The only problem I have not currently is that my selection box is not appearing when I click and drag

    updated code here

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraOperator : MonoBehaviour
    5. {
    6.     public Texture2D selectionHighlight = null;
    7.     public static Rect selection = new Rect(0,0,0,0);
    8.     private Vector3 startClick = -Vector3.one;
    9.  
    10.  
    11.     private void Update()
    12.     {
    13.         CheckCamera();
    14.     }
    15.  
    16.     private void CheckCamera()
    17.     {
    18.         if (Input.GetMouseButtonDown (0))
    19.             startClick = Input.mousePosition;
    20.         else if (Input.GetMouseButtonUp(0))
    21.         {
    22.             if (selection.width < 0)
    23.             {
    24.                 selection.x += selection.width;
    25.                 selection.width = -selection.width;
    26.             }
    27.             if (selection.height < 0)
    28.             {
    29.                 selection.y += selection.height;
    30.                 selection.height = -selection.height;
    31.             }
    32.  
    33.             startClick = -Vector3.one;
    34.       }
    35.  
    36.         if (Input.GetMouseButton(0))
    37.             selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));
    38.  
    39.    }
    40.        private void OnGUI()
    41.     {
    42.         if (startClick != -Vector3.one)
    43.         {
    44.             GUI.color = new Color (1, 1, 1, 0.5f);
    45.             GUI.DrawTexture (selection, selectionHighlight);
    46.         }
    47.     }
    48.  
    49.        public static float InvertMouseY(float y)
    50.         {
    51.                     return Screen.height - y;
    52.         }
    53. }
    54.  
    55.  
    56.  
     
    Last edited: Sep 27, 2016
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    its probably because your getting negative width and heights depending on which way you drag your mouse. I don't believe any of the Unity functions that take Rectangles handle negative width/height. You need to do that like this:
    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.             selection = new Rect(GetRectFromPoints(new Vector2(startClick.x, InvertMouseY(startClick.y)),
    3.                               new Vector2(Input.mousePosition.x,InvertMouseY(Input.mousePosition.y)));
    4.  
    5. Rect GetRectFromPoints(Vector2 p1, Vector2 p2)
    6. {
    7.          float minX = Mathf.Min(p1.x, p2.x);
    8.          float minY = Mathf.Min(p1.y,p2.y);
    9.          float width = Mathf.Abs(p1.x-p2.x);
    10.          float height = Mathf.Abs(p1.y-p2.y);
    11.  
    12.         return new Rect(minX,minY, width,height);
    13. }
     
  3. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Thanks I will try that and see if it works
     
  4. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Tried it but I keep getting these errors even though it doesnt look like the Rect GetRectFromPoints(Vector2 p1, Vector2 p2) should have a semicolon

    EDIT added an additional ) on the
    new Vector2(Input.mousePosition.x,InvertMouseY(Input.mousePosition.y))) line but I am still getting an error

    Assets/Scripts/CameraOperator.cs(42,55): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='

    This is the error that often annoys me the most since I often dont know what it wants exactly even when I add in everything
     

    Attached Files:

    Last edited: Sep 29, 2016
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    its missing a final ')' right before the semicolon. Just counted them and there is one missing on the end. Basically the closing parentheses for new Rect
     
  6. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Did that and the error went away but the error on the GetRectFromPoints line still remains

    upload_2016-9-29_9-15-13.png
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Three things you need a } at line 39 to end the Check Camera function

    And remove that ; at the end GetRectFromPoint

    And then remove the } at line 53. It should have been up at 39

    I think you were thinking my GetRectFromPoint was some function call. Its an entire method and shouldn't be inside CheckCamera Its a Utility method that takes a set of 4 points and returns a rectangle that has a positive width and height
     
  8. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay I tried that and it works almost the only error I have now is that I get an identifier expected message on GetRectFromPoints line

    EDIT never mind I found I had an extra , and I removed now everything should work fine now. I will post if I run into any more problems
     
  9. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay now when I go to test I get the following error

    null texture passed to GUI.DrawTexture
    UnityEngine.GUI DrawTexture(Rect, Texture)
    CameraOperator:OnGUI() (at Assets/Scripts/CameraOperator.cs:56)

    Oh wait I think I forgot to add a texture what would you recommend for a selection box?
     
  10. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay I decided to try a different method sorta for my selection box. I was reading around on some other threads and I found this method of converting a texture into a 2D sprite

    upload_2016-10-1_19-20-43.png
    This is what I am using for my selection box

    http://gamedev.stackexchange.com/questions/93458/creating-a-unit-selection-box-in-unity-2d

    I following the advice found here inj the link above



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpriteRenderer : MonoBehaviour {
    5.  
    6.     public SpriteRenderer bg;
    7.  
    8.     void change()
    9.     {
    10.         Texture 2D sprites = Resources.Load<Texture2D>("lev1/"name[i]) ;
    11.                 Rect rect = new Rect(0, 0, sprites.width, sprites.height);
    12.                 Sprite.Create(sprites,rect,new Vector2(0,0),1);
    13.                 bg.sprite = Sprite.Create(sprites,rect, new Vector2(0,0),.01f);}
    14.        
    15. }
    16.  
    And here is the sprite renderer I made based off the method shown in the link '

    I ran into the following error
    Assets/Scripts/SpriteRenderer.cs(10,26): error CS1525: Unexpected symbol `2'
     
  11. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This:
    Code (CSharp):
    1. Texture 2D
    Should be all one word:
    Code (CSharp):
    1. Texture2D
     
  12. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    New error
    Script 'SpriteRenderer' has the same name as built-in Unity component.
    AddComponent and GetComponent will not work with this script.
     
  13. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your Class can't be called SpriteRenderer because Unity already has a class callled SpriteRenderer. Call it SpriteController instead
     
  14. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Or use namespaces. With this example this might not be needed, but it's a good habit anyway and if you later add a second function that depends on this particular implementation of SpriteRenderer, it might break.
     
  15. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    upload_2016-10-2_11-33-38.png

    keep running into this error
     
  16. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    You might want to look up some C# tutorials - most of the errors you're seeing don't have anything to do with Unity, it's just syntax/scoping.

    This expression:
    "lev1/"name
    doesn't make sense in C#. Assuming name is actually accessible (since it's not declared in this scope), if you want to access element i (which is also not declared here) and put it as a string, you'd need "lev1/" + name.ToString() . If name is an array of strings, you can probably drop .ToString()

    Unfortunately once you solve this, there will probably be more scope errors in next lines (f.e. texture is not declared as far as I can tell).
     
  17. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    I am sorry I am a complete programming noob and I just follow what is shown in the tutorials
     
  18. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay I just decided to throw out that sprite renderer script and just go with the CameraOperator script I made previously and use the selection box for the texture
     
  19. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Don't feel sorry for starting out, everybody did.
    When going through tutorials, I'd also recommend to focus on why someone does what he does and how it works. If a tutorial doesn't explain the "why"'s and you can't figure it out yourself, you'll get lost. And then something is working weirdly and how to fix it get's convoluted etc.
    Hence why tutorials are good, try to look for those that explain not only what, but why, as otherwise they might bring as much bad as good (if that makes sense).
     
  20. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131


    I followed this tutorial on creating movement scripts no errors but the units dont move when I click after selecting them. I tried adding a nav mesh agent but it didnt seem to work. I think I need to add it to the plane