Search Unity

error cs1022 type or namespace definition

Discussion in 'Scripting' started by reefy86, Nov 19, 2017.

Thread Status:
Not open for further replies.
  1. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Have no idea why i keep getting errors when i follow tutorials exactly how they do it but mine never works lol. I am trying to have the sphere hidden when game starts but then activates when i hit a button on my touch controllers.

    Here is the error code.

    cheers



    GameObject Sphere;

    // Use this for initialization
    void Start () {
    Sphere.SetActive(false);

    }

    // Update is called once per frame
    void Update()
    {
    if (OVRInput.Get(OVRInput.Button.One))
    {
    Sphere.SetActive(true);
    }
    }
    }
     
  2. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    i fixed it i had one extra } at the bottom. i still can't manage to get the oculus controller to activate the sphere though :( and the sphere at the start is not being deactivated even though the code is telling it to.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    in the script you posted (btw, please look at this for how to post code on the forums : https://forum.unity3d.com/threads/using-code-tags-properly.143875/), the Sphere gameobject is private, so how is that assigned?
    Did you type it differently here ?
    If that's exactly how your project is, you want to try maybe setting public or the attribute "SerializeField" and then drag/drop the sphere game object into the script in the inspector.
     
    reefy86 likes this.
  4. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Thank you methos5k, i will read through that :) anyways i ended up sorting it, What i did not do was after assigning the GameObject to Sphere i did not know you had to drag the sphere inside unity to the inspector aswell. its tough but exciting work lol :)
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, glad you got it working :)

    And I know what you mean by "tough but exciting" lol
     
  6. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    just one quick one and i have really looked on the net but with no luck :( i can get an object to scale up but how can i set a maximum value so it stops scaling once a value is hit. Here is the code i am using to scale between .001 but would like it to stop at .1.

    Code (csharp):
    1.  
    2. this.Sphere.transform.localScale += new Vector3(.001f, .001f, .001f);
    3.  
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, so that is in the Update method or something?
    What you could maybe do in this particular situation (because it's even), is just do:
    Code (csharp):
    1.  
    2. float scaleValue = .001f;
    3.  
    4. void Update() {
    5.    scaleValue += .001f;
    6.    scaleValue = Mathf.Clamp(scaleValue, .001f, 1f);
    7.    Sphere.transform.localScale = new Vector3(scaleValue, scaleValue, scaleValue);
    8.   }
    9.  
    So as to not confuse things, I won't describe fully this next comment, but if your scaling just goes up at an event (or just once), it could be "slightly better" to do it in a coroutine. It's a little more organized, b/c then Update code is not always running, when not needed. For a small thing like this, and just learning, it's no big deal.. nothing to worry about :)
     
  8. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    thank you again, the scale happens everytime i press a button, so if i hold button a it keeps scaling until i let go but i wanted it to stop at a certain value no matter how long i hold the button, once i let go of the button it disappears. I have all this working apart from the maximum scale value as it just keeps getting bigger and bigger with no limit lol.

    thank you again i really appreciate it :)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay. well the code i wrote would help with stopping at the max value, too:)
     
  10. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    it doesnt seem to be working :( the sphere appears as normal when holding button down but its not scaling at all
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol, okay how about you post the script you're using? :) Maybe there is an issue somewhere.
     
  12. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    sorry to be a pain :(

    Code (csharp):
    1.  
    2. public GameObject Sphere;
    3.     public float scaleValue = .001f;
    4.  
    5.  
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.         Sphere.SetActive(false);
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.  
    17.         scaleValue += .001f;
    18.         scaleValue = Mathf.Clamp(scaleValue, .001f, 1f);
    19.         Sphere.transform.localScale = new Vector3(scaleValue, scaleValue, scaleValue);
    20.  
    21.         if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger))
    22.         {
    23.             if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
    24.                 Sphere.SetActive(true);
    25.         }
    26.         else
    27.             Sphere.SetActive(false);
    28.  
    29.  
    30.     }
    31. }
    32.  
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so first just to confirm this script is on an object that is not the sphere.
    Then, I had to use mouse buttons instead of your OVRInput, because I don't have that.

    Here's what I did:
    Code (csharp):
    1.  
    2.     void Update()
    3.     {
    4.         if (Input.GetMouseButton(0))
    5.         {
    6.             if (Input.GetMouseButton(1))
    7.             {
    8.                 Sphere.SetActive(true);
    9.                 scaleValue += .001f;
    10.                 scaleValue = Mathf.Clamp(scaleValue, .001f, 1f);
    11.                 Sphere.transform.localScale = new Vector3(scaleValue, scaleValue, scaleValue);
    12.             }
    13.         }
    14.         else
    15.             Sphere.SetActive(false);
    16.     }
    17.  
    So, here we have the scale going up so long as both buttons are held down (and the sphere is active).
    If both buttons aren't down, it's inactive (and also not scaling up).
    It respects the bounds -- doesn't scale higher than 1,1,1.

    Is that what you wanted? :)
     
  14. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    its working now thank you :) so in order for it to work you have to put the scale coding in after the sphere is set to true and then having the sphere set to false at the end to close it all off.

    again thank you so much
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, before it was scaling up as soon as the game started, without regard for whether the buttons were held down :) So, I suppose if you waited some time, it would be scaled up entirely before you realized. =)

    Also, 1 small note.. the code, as-is, is locked more to the frame rate than you might want.
    You could adjust it to use Time.deltaTime and some factor (combined) to get the scaling at a consistent time, instead.. :) If you want.
     
  16. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Thanks :) so something similar to this "float translation = Time.deltaTime * 10" with the 10 being seconds? if so do i add it to the same line as the transform.localscale
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    you could add it right into (this):
    Code (csharp):
    1. scaleValue += .001f; // here..
    2. // maybe like:
    3. scaleValue += Time.deltaTime;
    4.  
    When you multiply it by another number, it's like saying "I want x amount in a second". In your example, you'd be saying you want 10 in a second. So, you decide what value suits the speed you'd like. Just experiment and see :)
     
  18. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    works and seems to run alot smoother aswell :) is it possible to add time limits to multiple scales? what i mean is, lets say i hold the triggers down and the sphere grows from .001 to 100 in 10 seconds and then pauses for a further 10 seconds and then add another scale value?

    like .001-100 wait 10 seconds while still holding triggers then after 10 seconds it bumps from 100 to 200 and just keep repeating untill ive had enough :)

    sounds odd but what i have in my head seems to be working smoothly
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Glad it's working well...

    re: your other question..
    Well, that would be totally different than you're maximum value of 1 lol.

    Of course it's possible :) You'd have to write some code to start the pause you described when you reach your amount, and then check when that time is up, etc... :)
    Just think through your idea and try to write it out. heh.

    I wish ya luck with everything. take it easy. :)
     
  20. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    the 1 was just something to use for now :) ill figure it out and thanks again i really appreciate your help.

    take care
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, okay, cool :)

    You're welcome.
     
  22. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Hi meth, Hoping not to disturb you but if you have time could you help me on a little problem i have been trying to fix all day lol.

    same way i do with the sphere by setting it active true or false how would i do it with the canvas that has a bunch of text and images inside like below? its not working like the gameobject is.

    Untitled.png
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You mean by holding down 2 controls/buttons to make it active (otherwise inactive?)
    Is the script on anything in that screenshot?
     
  24. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    no this one is just going to activate on a single button just for practice purpose, once i learn this then i want to learn how to have it come active at a specific time in the game instead of pressing a button to activate it. I can get everything else to disable and enable just fine but because this is a canvas with a folder inside with images inside that folder, it become more trickier.

    Its actually an animated loading bar and the script is inside vica

    Untitled-1.jpg
     
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so the reason is that scripts don't execute Unity methods on deactivated game objects (or, in this case, its children). The same thing would happen on other game objects.
    What you can do is have the script somewhere else, maybe on an empty game object in the scene, and reference the canvas that way.
     
  26. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I may have misunderstood what you want going activate/inactive, but even if I did, just make sure that the script is not on an inactive game object (whether it's inactive itself or inactive b/c any parent is inactive)* :)
     
  27. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Thanks meth. i have created an empty object and assigned the script on that, i also dragged the canvas folder over too but still no look.

    Untitled.png




    Code (csharp):
    1.  
    2. public GameObject Canvas;
    3.     private RectTransform rectComponent;
    4.     private Image imageComp;
    5.  
    6.     public float speed = 200f;
    7.     public Text text;
    8.     public Text textNormal;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         Canvas.SetActive(false);
    15.         rectComponent = GetComponent<RectTransform>();
    16.         imageComp = rectComponent.GetComponent<Image>();
    17.         imageComp.fillAmount = 0.0f;
    18.  
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         if (OVRInput.GetDown(OVRInput.Button.One)) ;
    27.         Canvas.SetActive(true);
    28.  
    29.  
    30.         int a = 0;
    31.         if (imageComp.fillAmount != 1f)
    32.         {
    33.             imageComp.fillAmount = imageComp.fillAmount + Time.deltaTime * speed;
    34.             a = (int)(imageComp.fillAmount * 100);
    35.             if (a > 0 && a <= 33)
    36.             {
    37.                 textNormal.text = "Gathering Energy from Universes...";
    38.             }
    39.             else if (a > 33 && a <= 67)
    40.             {
    41.                 textNormal.text = "Press Button A once each bar is full...";
    42.             }
    43.             else if (a > 67 && a <= 100)
    44.             {
    45.                 textNormal.text = "Once all 4 bars are full, press both triggers...";
    46.             }
    47.             else
    48.             {
    49.  
    50.             }
    51.             text.text = a + "%";
    52.         }
    53.         else
    54.         {
    55.             imageComp.fillAmount = 0.0f;
    56.             text.text = "0%";
    57.         }
    58.     }
    59. }
    60.  
     
  28. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so if I take a half guess.. you want this fill amount to go up while you're holding the 1 button?
    You're missing some braces... :)

    Each 'if' statement must "wrap" what it wants to do.

    Maybe just pointing that out will be enough to fix it. If not, just write back and I'll explain more.
     
  29. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    what i really want to do is have the loading bar disabled from the start of the game and then set a timer for it to be enabled on its own instead of pressing any buttons to enable it. but for now i wanted to get the hang of coding so i was just doing it with a simple button:)
     
  30. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    ive tried adding it to different objects in the game but still no progress. so just to double check.

    place the script on an object
    create public GameObject loadingbar
    then put the canvas folder inside the inspector window
     
  31. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    managed to sort it but now the animation has stopped lol. getting there :)
     
  32. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You keep expanding your questions lol..

    but did my last post fix your previous situation for you? :)
     
  33. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    sorry:( dont want to keep asking questions, its getting addictive lol :) anyways yes your solution did work but the animation does not work. instead of it counting up its just a still image, all the animation seems to be in the vica file
     
  34. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay does the 'a' variable log the right information?
     
  35. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Provided you put the braces correctly, I think maybe your speed is way too fast. Try just Time.deltaTime without "* speed".
     
  36. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    well the loading bar counts up from 0% to 100% just fine if i don't mess around adding things to the script file. I just dont understand why just adding public GameObject loadingbar disables the counting up
     
  37. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I must be misunderstanding something..Can you export a package and post it in the thread? :)
     
  38. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    ive fixed it :) im not sure if this is normal but i have to have the script file attached to both the vica file and in my case i used the ovrplayercontrol and adding gameobject "canvas" to both the vica and the ovrplayercontroller. if i dont then the animation does not work and shows as a still image.

    Untitled-1.jpg
     
  39. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay.. well, glad you got it fixed anyways :)
     
    reefy86 likes this.
  40. reefy86

    reefy86

    Joined:
    Nov 18, 2017
    Posts:
    38
    Thank you :)
     
  41. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :) Enjoy making your game :)
     
  42. alpha1rider

    alpha1rider

    Joined:
    Jan 11, 2021
    Posts:
    25
    Hellow I am gettin the Error CS1022 can you help me
    This is my script please tell me what is wrong with it

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. {
    7.  
    8. }
    9.  
    10. public class Playercontroller: MonoBehaviour
    11. {
    12.     private Rigidbody rb;
    13.     private float movementX;
    14.     private float movementY;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     void Onmove(InputValue movementValue)
    23.     {
    24.         Vector2 movementVector = movementValue.Get<Vector2>();
    25.  
    26.         movementX = movementVector.X;
    27.         movementY = movementVector.Y;
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         Vector3 movement = new Vector3(movementX, 0.0f, movementY);
    33.  
    34.         rb.AddForce(movement);
    35.     }
    36. }
     
  43. RabbleHauser

    RabbleHauser

    Joined:
    Nov 4, 2021
    Posts:
    1
    I would try deleting the empty brackets at the top.
     
Thread Status:
Not open for further replies.